2019-05-26 11:54:32 -05:00
|
|
|
//
|
|
|
|
|
// ErrorHandler.swift
|
|
|
|
|
// NetNewsWire
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 5/26/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2026-08-02 10:33:23 -07:00
|
|
|
import RSCore
|
2019-05-26 11:54:32 -05:00
|
|
|
import AppKit
|
|
|
|
|
import Account
|
2026-03-23 11:23:15 -07:00
|
|
|
import os
|
2019-05-26 11:54:32 -05:00
|
|
|
|
2025-11-17 21:53:40 -08:00
|
|
|
struct ErrorHandler: Sendable {
|
2019-05-26 11:54:32 -05:00
|
|
|
|
2026-08-02 10:33:23 -07:00
|
|
|
private static let logger = Logger(subsystem: Logger.nnwSubsystem, category: "ErrorHandler")
|
2025-10-02 08:54:48 -07:00
|
|
|
|
2025-11-17 11:50:41 -08:00
|
|
|
@Sendable public static func present(_ error: Error) {
|
|
|
|
|
Task { @MainActor in
|
2026-07-25 21:59:14 -07:00
|
|
|
NSApplication.shared.presentError(errorForPresentation(error))
|
2025-11-17 11:50:41 -08:00
|
|
|
}
|
2019-05-26 11:54:32 -05:00
|
|
|
}
|
2025-10-02 08:54:48 -07:00
|
|
|
|
2019-06-10 15:21:17 -05:00
|
|
|
public static func log(_ error: Error) {
|
2025-10-02 08:54:48 -07:00
|
|
|
logger.error("\(error.localizedDescription)")
|
2019-06-10 15:21:17 -05:00
|
|
|
}
|
2026-07-25 21:59:14 -07:00
|
|
|
|
|
|
|
|
// Drop a recovery suggestion when there are no recovery options to act on it.
|
|
|
|
|
private static func errorForPresentation(_ error: Error) -> NSError {
|
|
|
|
|
let nsError = error as NSError
|
|
|
|
|
guard nsError.localizedRecoverySuggestion != nil, nsError.localizedRecoveryOptions == nil else {
|
|
|
|
|
return nsError
|
|
|
|
|
}
|
|
|
|
|
var userInfo = nsError.userInfo
|
|
|
|
|
userInfo[NSLocalizedDescriptionKey] = nsError.localizedDescription
|
|
|
|
|
userInfo[NSLocalizedRecoverySuggestionErrorKey] = nil
|
|
|
|
|
return NSError(domain: nsError.domain, code: nsError.code, userInfo: userInfo)
|
|
|
|
|
}
|
2019-05-26 11:54:32 -05:00
|
|
|
}
|