2020-03-11 13:35:14 -07:00
|
|
|
//
|
2020-08-27 16:39:03 -07:00
|
|
|
// NSError+AltStore.swift
|
2020-03-11 13:35:14 -07:00
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 3/11/20.
|
|
|
|
|
// Copyright © 2020 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
extension NSError
|
|
|
|
|
{
|
|
|
|
|
@objc(alt_localizedFailure)
|
|
|
|
|
var localizedFailure: String? {
|
|
|
|
|
let localizedFailure = (self.userInfo[NSLocalizedFailureErrorKey] as? String) ?? (NSError.userInfoValueProvider(forDomain: self.domain)?(self, NSLocalizedFailureErrorKey) as? String)
|
|
|
|
|
return localizedFailure
|
|
|
|
|
}
|
2020-03-24 13:27:44 -07:00
|
|
|
|
|
|
|
|
func withLocalizedFailure(_ failure: String) -> NSError
|
|
|
|
|
{
|
|
|
|
|
var userInfo = self.userInfo
|
|
|
|
|
userInfo[NSLocalizedFailureErrorKey] = failure
|
2020-05-15 11:39:03 -07:00
|
|
|
userInfo[NSLocalizedDescriptionKey] = self.localizedDescription
|
|
|
|
|
userInfo[NSLocalizedFailureReasonErrorKey] = self.localizedFailureReason
|
|
|
|
|
userInfo[NSLocalizedRecoverySuggestionErrorKey] = self.localizedRecoverySuggestion
|
2020-03-24 13:27:44 -07:00
|
|
|
|
|
|
|
|
let error = NSError(domain: self.domain, code: self.code, userInfo: userInfo)
|
|
|
|
|
return error
|
|
|
|
|
}
|
2020-08-27 16:39:03 -07:00
|
|
|
|
|
|
|
|
func sanitizedForCoreData() -> NSError
|
|
|
|
|
{
|
|
|
|
|
var userInfo = self.userInfo
|
|
|
|
|
userInfo[NSLocalizedFailureErrorKey] = self.localizedFailure
|
|
|
|
|
userInfo[NSLocalizedDescriptionKey] = self.localizedDescription
|
|
|
|
|
userInfo[NSLocalizedFailureReasonErrorKey] = self.localizedFailureReason
|
|
|
|
|
userInfo[NSLocalizedRecoverySuggestionErrorKey] = self.localizedRecoverySuggestion
|
|
|
|
|
|
|
|
|
|
// Remove non-ObjC-compliant userInfo values.
|
|
|
|
|
userInfo["NSCodingPath"] = nil
|
|
|
|
|
|
|
|
|
|
let error = NSError(domain: self.domain, code: self.code, userInfo: userInfo)
|
|
|
|
|
return error
|
|
|
|
|
}
|
2020-03-11 13:35:14 -07:00
|
|
|
}
|
2020-05-02 22:06:57 -07:00
|
|
|
|
2021-02-26 13:50:50 -06:00
|
|
|
extension Error
|
|
|
|
|
{
|
|
|
|
|
var underlyingError: Error? {
|
|
|
|
|
let underlyingError = (self as NSError).userInfo[NSUnderlyingErrorKey] as? Error
|
|
|
|
|
return underlyingError
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 22:06:57 -07:00
|
|
|
protocol ALTLocalizedError: LocalizedError, CustomNSError
|
|
|
|
|
{
|
2021-02-26 15:25:10 -06:00
|
|
|
var failure: String? { get }
|
2021-02-26 13:50:50 -06:00
|
|
|
|
|
|
|
|
var underlyingError: Error? { get }
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension ALTLocalizedError
|
|
|
|
|
{
|
|
|
|
|
var errorUserInfo: [String : Any] {
|
2021-02-26 13:50:50 -06:00
|
|
|
let userInfo = ([
|
|
|
|
|
NSLocalizedDescriptionKey: self.errorDescription,
|
|
|
|
|
NSLocalizedFailureReasonErrorKey: self.failureReason,
|
|
|
|
|
NSLocalizedFailureErrorKey: self.failure,
|
|
|
|
|
NSUnderlyingErrorKey: self.underlyingError
|
|
|
|
|
] as [String: Any?]).compactMapValues { $0 }
|
2020-05-02 22:06:57 -07:00
|
|
|
return userInfo
|
|
|
|
|
}
|
2021-02-26 13:50:50 -06:00
|
|
|
|
|
|
|
|
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 }
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|