mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-15 09:43:34 +01:00
Adds ALTLocalizedError.underlyingError
Allows for easily wrapping underlying errors while preserving localized descriptions.
This commit is contained in:
@@ -49,7 +49,7 @@ class ToastView: RSTToastView
|
|||||||
convenience init(error: Error)
|
convenience init(error: Error)
|
||||||
{
|
{
|
||||||
var error = error as NSError
|
var error = error as NSError
|
||||||
var underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError
|
var underlyingError = error.underlyingError
|
||||||
|
|
||||||
var preferredDuration: TimeInterval?
|
var preferredDuration: TimeInterval?
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ class ToastView: RSTToastView
|
|||||||
{
|
{
|
||||||
// Treat underlyingError as the primary error.
|
// Treat underlyingError as the primary error.
|
||||||
|
|
||||||
error = unwrappedUnderlyingError
|
error = unwrappedUnderlyingError as NSError
|
||||||
underlyingError = nil
|
underlyingError = nil
|
||||||
|
|
||||||
preferredDuration = .longToastViewDuration
|
preferredDuration = .longToastViewDuration
|
||||||
|
|||||||
@@ -44,17 +44,49 @@ extension NSError
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Error
|
||||||
|
{
|
||||||
|
var underlyingError: Error? {
|
||||||
|
let underlyingError = (self as NSError).userInfo[NSUnderlyingErrorKey] as? Error
|
||||||
|
return underlyingError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protocol ALTLocalizedError: LocalizedError, CustomNSError
|
protocol ALTLocalizedError: LocalizedError, CustomNSError
|
||||||
{
|
{
|
||||||
var failure: String? { get }
|
var failure: String? { get }
|
||||||
|
|
||||||
|
var underlyingError: Error? { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ALTLocalizedError
|
extension ALTLocalizedError
|
||||||
{
|
{
|
||||||
var errorUserInfo: [String : Any] {
|
var errorUserInfo: [String : Any] {
|
||||||
let userInfo = [NSLocalizedDescriptionKey: self.errorDescription,
|
let userInfo = ([
|
||||||
NSLocalizedFailureReasonErrorKey: self.failureReason,
|
NSLocalizedDescriptionKey: self.errorDescription,
|
||||||
NSLocalizedFailureErrorKey: self.failure].compactMapValues { $0 }
|
NSLocalizedFailureReasonErrorKey: self.failureReason,
|
||||||
|
NSLocalizedFailureErrorKey: self.failure,
|
||||||
|
NSUnderlyingErrorKey: self.underlyingError
|
||||||
|
] as [String: Any?]).compactMapValues { $0 }
|
||||||
return userInfo
|
return userInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var underlyingError: Error? {
|
||||||
|
// Error's default implementation calls errorUserInfo,
|
||||||
|
// but ALTLocalizedError.errorUserInfo calls underlyingError.
|
||||||
|
// Return nil to prevent infinite recursion.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
guard let errorFailure = self.failure else { return (self.underlyingError as NSError?)?.localizedDescription }
|
||||||
|
guard let failureReason = self.failureReason else { return errorFailure }
|
||||||
|
|
||||||
|
let errorDescription = errorFailure + " " + failureReason
|
||||||
|
return errorDescription
|
||||||
|
}
|
||||||
|
|
||||||
|
var failureReason: String? { (self.underlyingError as NSError?)?.localizedFailureReason ?? (self.underlyingError as NSError?)?.localizedDescription }
|
||||||
|
var recoverySuggestion: String? { (self.underlyingError as NSError?)?.localizedRecoverySuggestion }
|
||||||
|
var helpAnchor: String? { (self.underlyingError as NSError?)?.helpAnchor }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user