mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-14 17:23:25 +01:00
[Shared] Refactors error handling based on ALTLocalizedError protocol (#1115)
* [Shared] Revises ALTLocalizedError protocol * Refactors errors to conform to revised ALTLocalizedError protocol * [Missing Commit] Remaining changes for ALTLocalizedError * [AltServer] Refactors errors to conform to revised ALTLocalizedError protocol * [Missing Commit] Declares ALTLocalizedTitleErrorKey + ALTLocalizedDescriptionKey * Updates Objective-C errors to match revised ALTLocalizedError * [Missing Commit] Unnecessary ALTLocalizedDescription logic * [Shared] Refactors NSError.withLocalizedFailure to properly support ALTLocalizedError * [Shared] Supports adding localized titles to errors via NSError.withLocalizedTitle() * Revises ErrorResponse logic to support arbitrary errors and user info values * [Missed Commit] Renames CodableServerError to CodableError * Merges ConnectionError into OperationError * [Missed Commit] Doesn’t check ALTWrappedError’s userInfo for localizedDescription * [Missed] Fixes incorrect errorDomain for ALTErrorEnums * [Missed] Removes nonexistent ALTWrappedError.h * Includes source file and line number in OperationError.unknown failureReason * Adds localizedTitle to AppManager operation errors * Fixes adding localizedTitle + localizedFailure to ALTWrappedError * Updates ToastView to use error’s localizedTitle as title * [Shared] Adds NSError.formattedDetailedDescription(with:) Returns formatted NSAttributedString containing all user info values intended for displaying to the user. * [Shared] Updates Error.localizedErrorCode to say “code” instead of “error” * Conforms ALTLocalizedError to CustomStringConvertible * Adds “View More Details” option to Error Log context menu to view detailed error description * [Shared] Fixes NSError.formattedDetailedDescription appearing black in dark mode * [AltServer] Updates error alert to match revised error logic Uses error’s localizedTitle as alert title. * [AltServer] Adds “View More Details” button to error alert to view detailed error info * [AltServer] Renames InstallError to OperationError and conforms to ALTErrorEnum * [Shared] Removes CodableError support for Date user info values Not currently used, and we don’t want to accidentally parse a non-Date as a Date in the meantime. * [Shared] Includes dynamic UserInfoValueProvider values in NSError.formattedDetailedDescription() * [Shared] Includes source file + line in NSError.formattedDetailedDescription() Automatically captures source file + line when throwing ALTErrorEnums. * [Shared] Captures source file + line for unknown errors * Removes sourceFunction from OperationError * Adds localizedTitle to AuthenticationViewController errors * [Shared] Moves nested ALTWrappedError logic to ALTWrappedError initializer * [AltServer] Removes now-redundant localized failure from JIT errors All JIT errors now have a localizedTitle which effectively says the same thing. * Makes OperationError.Code start at 1000 “Connection errors” subsection starts at 1200. * [Shared] Updates Error domains to revised [Source].[ErrorType] format * Updates ALTWrappedError.localizedDescription to prioritize using wrapped NSLocalizedDescription as failure reason * Makes ALTAppleAPIError codes start at 3000 * [AltServer] Adds relevant localizedFailures to ALTDeviceManager.installApplication() errors * Revises OperationError failureReasons and recovery suggestions All failure reasons now read correctly when preceded by a failure reason and “because”. * Revises ALTServerError error messages All failure reasons now read correctly when preceded by a failure reason and “because”. * Most failure reasons now read correctly when preceded by a failure reason and “because”. * ALTServerErrorUnderlyingError forwards all user info provider calls to underlying error. * Revises error messages for ALTAppleAPIErrorIncorrectCredentials * [Missed] Removes NSError+AltStore.swift from AltStore target * [Shared] Updates AltServerErrorDomain to revised [Source].[ErrorType] format * [Shared] Removes “code” from Error.localizedErrorCode * [Shared] Makes ALTServerError codes (appear to) start at 2000 We can’t change the actual error codes without breaking backwards compatibility, so instead we just add 2000 whenever we display ALTServerError codes to the user. * Moves VerificationError.errorFailure to VerifyAppOperation * Supports custom failure reason for OperationError.unknown * [Shared] Changes AltServerErrorDomain to “AltServer.ServerError” * [Shared] Converts ALTWrappedError to Objective-C class NSError subclasses must be written in ObjC for Swift.Error <-> NSError bridging to work correctly. # Conflicts: # AltStore.xcodeproj/project.pbxproj * Fixes decoding CodableError nested user info values
This commit is contained in:
@@ -8,9 +8,15 @@
|
||||
|
||||
#import "NSError+ALTServerError.h"
|
||||
|
||||
NSErrorDomain const AltServerErrorDomain = @"com.rileytestut.AltServer";
|
||||
NSErrorDomain const AltServerInstallationErrorDomain = @"com.rileytestut.AltServer.Installation";
|
||||
NSErrorDomain const AltServerConnectionErrorDomain = @"com.rileytestut.AltServer.Connection";
|
||||
#if TARGET_OS_OSX
|
||||
#import "AltServer-Swift.h"
|
||||
#else
|
||||
#import <AltStoreCore/AltStoreCore-Swift.h>
|
||||
#endif
|
||||
|
||||
NSErrorDomain const AltServerErrorDomain = @"AltServer.ServerError";
|
||||
NSErrorDomain const AltServerInstallationErrorDomain = @"Apple.InstallationError";
|
||||
NSErrorDomain const AltServerConnectionErrorDomain = @"AltServer.ConnectionError";
|
||||
|
||||
NSErrorUserInfoKey const ALTUnderlyingErrorDomainErrorKey = @"underlyingErrorDomain";
|
||||
NSErrorUserInfoKey const ALTUnderlyingErrorCodeErrorKey = @"underlyingErrorCode";
|
||||
@@ -24,8 +30,16 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
|
||||
+ (void)load
|
||||
{
|
||||
[NSError setUserInfoValueProviderForDomain:AltServerErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
|
||||
if ([userInfoKey isEqualToString:NSLocalizedFailureReasonErrorKey])
|
||||
[NSError alt_setUserInfoValueProviderForDomain:AltServerErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
|
||||
if ([userInfoKey isEqualToString:NSLocalizedDescriptionKey])
|
||||
{
|
||||
return [error altserver_localizedDescription];
|
||||
}
|
||||
else if ([userInfoKey isEqualToString:NSLocalizedFailureErrorKey])
|
||||
{
|
||||
return [error altserver_localizedFailure];
|
||||
}
|
||||
else if ([userInfoKey isEqualToString:NSLocalizedFailureReasonErrorKey])
|
||||
{
|
||||
return [error altserver_localizedFailureReason];
|
||||
}
|
||||
@@ -41,10 +55,10 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
return nil;
|
||||
}];
|
||||
|
||||
[NSError setUserInfoValueProviderForDomain:AltServerConnectionErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
|
||||
if ([userInfoKey isEqualToString:NSLocalizedDescriptionKey])
|
||||
[NSError alt_setUserInfoValueProviderForDomain:AltServerConnectionErrorDomain provider:^id _Nullable(NSError * _Nonnull error, NSErrorUserInfoKey _Nonnull userInfoKey) {
|
||||
if ([userInfoKey isEqualToString:NSLocalizedFailureReasonErrorKey])
|
||||
{
|
||||
return [error altserver_connection_localizedDescription];
|
||||
return [error altserver_connection_localizedFailureReason];
|
||||
}
|
||||
else if ([userInfoKey isEqualToString:NSLocalizedRecoverySuggestionErrorKey])
|
||||
{
|
||||
@@ -55,6 +69,53 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
}];
|
||||
}
|
||||
|
||||
- (nullable NSString *)altserver_localizedDescription
|
||||
{
|
||||
switch ((ALTServerError)self.code)
|
||||
{
|
||||
case ALTServerErrorUnderlyingError:
|
||||
{
|
||||
// We're wrapping another error, so return the wrapped error's localized description.
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
return underlyingError.localizedDescription;
|
||||
}
|
||||
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable NSString *)altserver_localizedFailure
|
||||
{
|
||||
switch ((ALTServerError)self.code)
|
||||
{
|
||||
case ALTServerErrorUnderlyingError:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
return underlyingError.alt_localizedFailure;
|
||||
}
|
||||
|
||||
case ALTServerErrorConnectionFailed:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
if (underlyingError.localizedFailureReason != nil)
|
||||
{
|
||||
// Only return localized failure if there is an underlying error with failure reason.
|
||||
#if TARGET_OS_OSX
|
||||
return NSLocalizedString(@"There was an error connecting to the device.", @"");
|
||||
#else
|
||||
return NSLocalizedString(@"AltServer could not establish a connection to AltStore.", @"");
|
||||
#endif
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable NSString *)altserver_localizedFailureReason
|
||||
{
|
||||
switch ((ALTServerError)self.code)
|
||||
@@ -73,6 +134,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
return [NSString stringWithFormat:NSLocalizedString(@"Error code: %@", @""), underlyingErrorCode];
|
||||
}
|
||||
|
||||
// Return nil because this is a "pass-through" error, so if underlyingError doesn't have failure reason, this doesn't either.
|
||||
return nil;
|
||||
}
|
||||
|
||||
@@ -80,20 +142,30 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
return NSLocalizedString(@"An unknown error occured.", @"");
|
||||
|
||||
case ALTServerErrorConnectionFailed:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
if (underlyingError.localizedFailureReason != nil)
|
||||
{
|
||||
return underlyingError.localizedFailureReason;
|
||||
}
|
||||
|
||||
// Return fallback failure reason if there isn't an underlying error with failure reason.
|
||||
|
||||
#if TARGET_OS_OSX
|
||||
return NSLocalizedString(@"There was an error connecting to the device.", @"");
|
||||
#else
|
||||
return NSLocalizedString(@"Could not connect to AltServer.", @"");
|
||||
return NSLocalizedString(@"AltServer could not establish a connection to AltStore.", @"");
|
||||
#endif
|
||||
}
|
||||
|
||||
case ALTServerErrorLostConnection:
|
||||
return NSLocalizedString(@"Lost connection to AltServer.", @"");
|
||||
return NSLocalizedString(@"The connection to AltServer was lost.", @"");
|
||||
|
||||
case ALTServerErrorDeviceNotFound:
|
||||
return NSLocalizedString(@"AltServer could not find this device.", @"");
|
||||
|
||||
case ALTServerErrorDeviceWriteFailed:
|
||||
return NSLocalizedString(@"Failed to write app data to device.", @"");
|
||||
return NSLocalizedString(@"AltServer could not write data to this device.", @"");
|
||||
|
||||
case ALTServerErrorInvalidRequest:
|
||||
return NSLocalizedString(@"AltServer received an invalid request.", @"");
|
||||
@@ -102,13 +174,21 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
return NSLocalizedString(@"AltServer sent an invalid response.", @"");
|
||||
|
||||
case ALTServerErrorInvalidApp:
|
||||
return NSLocalizedString(@"The app is invalid.", @"");
|
||||
return NSLocalizedString(@"The app is in an invalid format.", @"");
|
||||
|
||||
case ALTServerErrorInstallationFailed:
|
||||
return NSLocalizedString(@"An error occured while installing the app.", @"");
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
if (underlyingError != nil)
|
||||
{
|
||||
return underlyingError.localizedFailureReason ?: underlyingError.localizedDescription;
|
||||
}
|
||||
|
||||
return NSLocalizedString(@"An error occurred while installing the app.", @"");
|
||||
}
|
||||
|
||||
case ALTServerErrorMaximumFreeAppLimitReached:
|
||||
return NSLocalizedString(@"Cannot activate more than 3 apps with a non-developer Apple ID.", @"");
|
||||
return NSLocalizedString(@"You cannot activate more than 3 apps with a non-developer Apple ID.", @"");
|
||||
|
||||
case ALTServerErrorUnsupportediOSVersion:
|
||||
return NSLocalizedString(@"Your device must be running iOS 12.2 or later to install AltStore.", @"");
|
||||
@@ -117,7 +197,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
return NSLocalizedString(@"AltServer does not support this request.", @"");
|
||||
|
||||
case ALTServerErrorUnknownResponse:
|
||||
return NSLocalizedString(@"Received an unknown response from AltServer.", @"");
|
||||
return NSLocalizedString(@"AltStore received an unknown response from AltServer.", @"");
|
||||
|
||||
case ALTServerErrorInvalidAnisetteData:
|
||||
return NSLocalizedString(@"The provided anisette data is invalid.", @"");
|
||||
@@ -141,7 +221,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
case ALTServerErrorIncompatibleDeveloperDisk:
|
||||
{
|
||||
NSString *osVersion = [self altserver_osVersion] ?: NSLocalizedString(@"this device's OS version", @"");
|
||||
NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"The disk is incompatible with %@.", @""), osVersion];
|
||||
NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"The disk is incompatible with %@.", @""), osVersion]; // "Developer" disk is included in localizedFailure
|
||||
return failureReason;
|
||||
}
|
||||
}
|
||||
@@ -153,7 +233,24 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
{
|
||||
switch ((ALTServerError)self.code)
|
||||
{
|
||||
case ALTServerErrorUnderlyingError:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
return underlyingError.localizedRecoverySuggestion;
|
||||
}
|
||||
|
||||
case ALTServerErrorConnectionFailed:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
if (underlyingError.localizedRecoverySuggestion != nil)
|
||||
{
|
||||
return underlyingError.localizedRecoverySuggestion;
|
||||
}
|
||||
|
||||
// If there is no underlying error, fall through to ALTServerErrorDeviceNotFound.
|
||||
// return nil;
|
||||
}
|
||||
|
||||
case ALTServerErrorDeviceNotFound:
|
||||
return NSLocalizedString(@"Make sure you have trusted this device with your computer and WiFi sync is enabled.", @"");
|
||||
|
||||
@@ -182,6 +279,12 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
{
|
||||
switch ((ALTServerError)self.code)
|
||||
{
|
||||
case ALTServerErrorUnderlyingError:
|
||||
{
|
||||
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
|
||||
return underlyingError.alt_localizedDebugDescription;
|
||||
}
|
||||
|
||||
case ALTServerErrorIncompatibleDeveloperDisk:
|
||||
{
|
||||
NSString *path = self.userInfo[NSFilePathErrorKey];
|
||||
@@ -191,7 +294,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
}
|
||||
|
||||
NSString *osVersion = [self altserver_osVersion] ?: NSLocalizedString(@"this device's OS version", @"");
|
||||
NSString *debugDescription = [NSString stringWithFormat:NSLocalizedString(@"The Developer disk located at\n\n%@\n\nis incompatible with %@.", @""), path, osVersion];
|
||||
NSString *debugDescription = [NSString stringWithFormat:NSLocalizedString(@"The Developer disk located at %@ is incompatible with %@.", @""), path, osVersion];
|
||||
return debugDescription;
|
||||
}
|
||||
|
||||
@@ -232,7 +335,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
|
||||
|
||||
#pragma mark - AltServerConnectionErrorDomain -
|
||||
|
||||
- (nullable NSString *)altserver_connection_localizedDescription
|
||||
- (nullable NSString *)altserver_connection_localizedFailureReason
|
||||
{
|
||||
switch ((ALTServerConnectionError)self.code)
|
||||
{
|
||||
|
||||
182
Shared/Errors/ALTLocalizedError.swift
Normal file
182
Shared/Errors/ALTLocalizedError.swift
Normal file
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// ALTLocalizedError.swift
|
||||
// AltStore
|
||||
//
|
||||
// Created by Riley Testut on 10/14/22.
|
||||
// Copyright © 2022 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AltSign
|
||||
|
||||
public let ALTLocalizedTitleErrorKey = "ALTLocalizedTitle"
|
||||
public let ALTLocalizedDescriptionKey = "ALTLocalizedDescription"
|
||||
|
||||
public protocol ALTLocalizedError<Code>: LocalizedError, CustomNSError, CustomStringConvertible
|
||||
{
|
||||
associatedtype Code: ALTErrorCode
|
||||
|
||||
var code: Code { get }
|
||||
var errorFailureReason: String { get }
|
||||
|
||||
var errorTitle: String? { get set }
|
||||
var errorFailure: String? { get set }
|
||||
|
||||
var sourceFile: String? { get set }
|
||||
var sourceLine: UInt? { get set }
|
||||
}
|
||||
|
||||
public extension ALTLocalizedError
|
||||
{
|
||||
var sourceFile: String? {
|
||||
get { nil }
|
||||
set {}
|
||||
}
|
||||
|
||||
var sourceLine: UInt? {
|
||||
get { nil }
|
||||
set {}
|
||||
}
|
||||
}
|
||||
|
||||
public protocol ALTErrorCode: RawRepresentable where RawValue == Int
|
||||
{
|
||||
associatedtype Error: ALTLocalizedError where Error.Code == Self
|
||||
|
||||
static var errorDomain: String { get } // Optional
|
||||
}
|
||||
|
||||
public protocol ALTErrorEnum: ALTErrorCode
|
||||
{
|
||||
associatedtype Error = DefaultLocalizedError<Self>
|
||||
|
||||
var errorFailureReason: String { get }
|
||||
}
|
||||
|
||||
/// LocalizedError & CustomNSError & CustomStringConvertible
|
||||
public extension ALTLocalizedError
|
||||
{
|
||||
var errorCode: Int { self.code.rawValue }
|
||||
|
||||
var errorDescription: String? {
|
||||
guard (self as NSError).localizedFailure == nil else {
|
||||
// Error has localizedFailure, so return nil to construct localizedDescription from it + localizedFailureReason.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Otherwise, return failureReason for localizedDescription to avoid system prepending "Operation Failed" message.
|
||||
return self.failureReason
|
||||
}
|
||||
|
||||
var failureReason: String? {
|
||||
return self.errorFailureReason
|
||||
}
|
||||
|
||||
var errorUserInfo: [String : Any] {
|
||||
let userInfo: [String: Any?] = [
|
||||
NSLocalizedFailureErrorKey: self.errorFailure,
|
||||
ALTLocalizedTitleErrorKey: self.errorTitle,
|
||||
ALTSourceFileErrorKey: self.sourceFile,
|
||||
ALTSourceLineErrorKey: self.sourceLine,
|
||||
]
|
||||
|
||||
return userInfo.compactMapValues { $0 }
|
||||
}
|
||||
|
||||
var description: String {
|
||||
let description = "\(self.localizedErrorCode) “\(self.localizedDescription)”"
|
||||
return description
|
||||
}
|
||||
}
|
||||
|
||||
/// Default Implementations
|
||||
public extension ALTLocalizedError where Code: ALTErrorEnum
|
||||
{
|
||||
static var errorDomain: String {
|
||||
return Code.errorDomain
|
||||
}
|
||||
|
||||
// ALTErrorEnum Codes provide their failure reason directly.
|
||||
var errorFailureReason: String {
|
||||
return self.code.errorFailureReason
|
||||
}
|
||||
}
|
||||
|
||||
/// Default Implementations
|
||||
public extension ALTErrorCode
|
||||
{
|
||||
static var errorDomain: String {
|
||||
let typeName = String(reflecting: Self.self) // "\(Self.self)" doesn't include module name, but String(reflecting:) does.
|
||||
let errorDomain = typeName.replacingOccurrences(of: "ErrorCode", with: "Error")
|
||||
return errorDomain
|
||||
}
|
||||
}
|
||||
|
||||
public extension ALTLocalizedError
|
||||
{
|
||||
// Allows us to initialize errors with localizedTitle + localizedFailure
|
||||
// while still using the error's custom initializer at callsite.
|
||||
init(_ error: Self, localizedTitle: String? = nil, localizedFailure: String? = nil)
|
||||
{
|
||||
self = error
|
||||
|
||||
if let localizedTitle
|
||||
{
|
||||
self.errorTitle = localizedTitle
|
||||
}
|
||||
|
||||
if let localizedFailure
|
||||
{
|
||||
self.errorFailure = localizedFailure
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultLocalizedError<Code: ALTErrorEnum>: ALTLocalizedError
|
||||
{
|
||||
public let code: Code
|
||||
|
||||
public var errorTitle: String?
|
||||
public var errorFailure: String?
|
||||
public var sourceFile: String?
|
||||
public var sourceLine: UInt?
|
||||
|
||||
public init(_ code: Code, localizedTitle: String? = nil, localizedFailure: String? = nil, sourceFile: String? = #fileID, sourceLine: UInt? = #line)
|
||||
{
|
||||
self.code = code
|
||||
self.errorTitle = localizedTitle
|
||||
self.errorFailure = localizedFailure
|
||||
self.sourceFile = sourceFile
|
||||
self.sourceLine = sourceLine
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom Operators
|
||||
/// These allow us to pattern match ALTErrorCodes against arbitrary errors via ~ prefix.
|
||||
prefix operator ~
|
||||
public prefix func ~<Code: ALTErrorCode>(expression: Code) -> NSError
|
||||
{
|
||||
let nsError = NSError(domain: Code.errorDomain, code: expression.rawValue)
|
||||
return nsError
|
||||
}
|
||||
|
||||
public func ~=(pattern: any Swift.Error, value: any Swift.Error) -> Bool
|
||||
{
|
||||
let isMatch = pattern._domain == value._domain && pattern._code == value._code
|
||||
return isMatch
|
||||
}
|
||||
|
||||
// These operators *should* allow us to match ALTErrorCodes against arbitrary errors,
|
||||
// but they don't work as of iOS 16.1 and Swift 5.7.
|
||||
//
|
||||
//public func ~=<Error: ALTLocalizedError>(pattern: Error, value: Swift.Error) -> Bool
|
||||
//{
|
||||
// let isMatch = pattern._domain == value._domain && pattern._code == value._code
|
||||
// return isMatch
|
||||
//}
|
||||
//
|
||||
//public func ~=<Code: ALTErrorCode>(pattern: Code, value: Swift.Error) -> Bool
|
||||
//{
|
||||
// let isMatch = Code.errorDomain == value._domain && pattern.rawValue == value._code
|
||||
// return isMatch
|
||||
//}
|
||||
25
Shared/Errors/ALTWrappedError.h
Normal file
25
Shared/Errors/ALTWrappedError.h
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// ALTWrappedError.h
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 11/28/22.
|
||||
// Copyright © 2022 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
// Overrides localizedDescription to check userInfoValueProvider for failure reason
|
||||
// instead of default behavior which just returns NSLocalizedFailureErrorKey if present.
|
||||
//
|
||||
// Must be written in Objective-C for Swift.Error <-> NSError bridging to work correctly.
|
||||
@interface ALTWrappedError : NSError
|
||||
|
||||
@property (copy, nonatomic) NSError *wrappedError;
|
||||
|
||||
- (instancetype)initWithError:(NSError *)error userInfo:(NSDictionary<NSString *, id> *)userInfo;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
73
Shared/Errors/ALTWrappedError.m
Normal file
73
Shared/Errors/ALTWrappedError.m
Normal file
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// ALTWrappedError.m
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 11/28/22.
|
||||
// Copyright © 2022 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ALTWrappedError.h"
|
||||
|
||||
@implementation ALTWrappedError
|
||||
|
||||
+ (BOOL)supportsSecureCoding
|
||||
{
|
||||
// Required in order to serialize errors for legacy AltServer communication.
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (instancetype)initWithError:(NSError *)error userInfo:(NSDictionary<NSString *,id> *)userInfo
|
||||
{
|
||||
self = [super initWithDomain:error.domain code:error.code userInfo:userInfo];
|
||||
if (self)
|
||||
{
|
||||
if ([error isKindOfClass:[ALTWrappedError class]])
|
||||
{
|
||||
_wrappedError = [(ALTWrappedError *)error wrappedError];
|
||||
}
|
||||
else
|
||||
{
|
||||
_wrappedError = [error copy];
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)localizedDescription
|
||||
{
|
||||
NSString *localizedFailure = self.userInfo[NSLocalizedFailureErrorKey];
|
||||
if (localizedFailure != nil)
|
||||
{
|
||||
NSString *wrappedLocalizedDescription = self.wrappedError.userInfo[NSLocalizedDescriptionKey];
|
||||
NSString *localizedFailureReason = wrappedLocalizedDescription ?: self.wrappedError.localizedFailureReason ?: self.wrappedError.localizedDescription;
|
||||
|
||||
NSString *localizedDescription = [NSString stringWithFormat:@"%@ %@", localizedFailure, localizedFailureReason];
|
||||
return localizedDescription;
|
||||
}
|
||||
|
||||
// localizedFailure is nil, so return wrappedError's localizedDescription.
|
||||
return self.wrappedError.localizedDescription;
|
||||
}
|
||||
|
||||
- (NSString *)localizedFailureReason
|
||||
{
|
||||
return self.wrappedError.localizedFailureReason;
|
||||
}
|
||||
|
||||
- (NSString *)localizedRecoverySuggestion
|
||||
{
|
||||
return self.wrappedError.localizedRecoverySuggestion;
|
||||
}
|
||||
|
||||
- (NSString *)debugDescription
|
||||
{
|
||||
return self.wrappedError.debugDescription;
|
||||
}
|
||||
|
||||
- (NSString *)helpAnchor
|
||||
{
|
||||
return self.wrappedError.helpAnchor;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -21,12 +21,10 @@ public extension ALTServerError
|
||||
case is DecodingError: self = ALTServerError(.invalidRequest, underlyingError: error)
|
||||
case is EncodingError: self = ALTServerError(.invalidResponse, underlyingError: error)
|
||||
case let error as NSError:
|
||||
// Assign error as underlying error, even if there already is one,
|
||||
// because it'll still be accessible via error.underlyingError.underlyingError.
|
||||
var userInfo = error.userInfo
|
||||
if !userInfo.keys.contains(NSUnderlyingErrorKey)
|
||||
{
|
||||
// Assign underlying error (if there isn't already one).
|
||||
userInfo[NSUnderlyingErrorKey] = error
|
||||
}
|
||||
userInfo[NSUnderlyingErrorKey] = error
|
||||
|
||||
self = ALTServerError(.underlyingError, userInfo: userInfo)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,17 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
extension NSError
|
||||
#if canImport(UIKit)
|
||||
import UIKit
|
||||
public typealias ALTFont = UIFont
|
||||
#elseif canImport(AppKit)
|
||||
import AppKit
|
||||
public typealias ALTFont = NSFont
|
||||
#endif
|
||||
|
||||
import AltSign
|
||||
|
||||
public extension NSError
|
||||
{
|
||||
@objc(alt_localizedFailure)
|
||||
var localizedFailure: String? {
|
||||
@@ -22,50 +32,56 @@ extension NSError
|
||||
return debugDescription
|
||||
}
|
||||
|
||||
@objc(alt_localizedTitle)
|
||||
var localizedTitle: String? {
|
||||
let localizedTitle = self.userInfo[ALTLocalizedTitleErrorKey] as? String
|
||||
return localizedTitle
|
||||
}
|
||||
|
||||
@objc(alt_errorWithLocalizedFailure:)
|
||||
func withLocalizedFailure(_ failure: String) -> NSError
|
||||
{
|
||||
var userInfo = self.userInfo
|
||||
userInfo[NSLocalizedFailureErrorKey] = failure
|
||||
|
||||
if let failureReason = self.localizedFailureReason
|
||||
switch self
|
||||
{
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = failureReason
|
||||
case var error as any ALTLocalizedError:
|
||||
error.errorFailure = failure
|
||||
return error as NSError
|
||||
|
||||
default:
|
||||
var userInfo = self.userInfo
|
||||
userInfo[NSLocalizedFailureErrorKey] = failure
|
||||
|
||||
let error = ALTWrappedError(error: self, userInfo: userInfo)
|
||||
return error
|
||||
}
|
||||
else if self.localizedFailure == nil && self.localizedFailureReason == nil && self.localizedDescription.contains(self.localizedErrorCode)
|
||||
{
|
||||
// Default localizedDescription, so replace with just the localized error code portion.
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = "(\(self.localizedErrorCode).)"
|
||||
}
|
||||
else
|
||||
{
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = self.localizedDescription
|
||||
}
|
||||
|
||||
if let localizedDescription = NSError.userInfoValueProvider(forDomain: self.domain)?(self, NSLocalizedDescriptionKey) as? String
|
||||
{
|
||||
userInfo[NSLocalizedDescriptionKey] = localizedDescription
|
||||
}
|
||||
|
||||
// Don't accidentally remove localizedDescription from dictionary
|
||||
// userInfo[NSLocalizedDescriptionKey] = NSError.userInfoValueProvider(forDomain: self.domain)?(self, NSLocalizedDescriptionKey) as? String
|
||||
|
||||
if let recoverySuggestion = self.localizedRecoverySuggestion
|
||||
{
|
||||
userInfo[NSLocalizedRecoverySuggestionErrorKey] = recoverySuggestion
|
||||
}
|
||||
|
||||
let error = NSError(domain: self.domain, code: self.code, userInfo: userInfo)
|
||||
return error
|
||||
}
|
||||
|
||||
func sanitizedForCoreData() -> NSError
|
||||
@objc(alt_errorWithLocalizedTitle:)
|
||||
func withLocalizedTitle(_ title: String) -> NSError
|
||||
{
|
||||
switch self
|
||||
{
|
||||
case var error as any ALTLocalizedError:
|
||||
error.errorTitle = title
|
||||
return error as NSError
|
||||
|
||||
default:
|
||||
var userInfo = self.userInfo
|
||||
userInfo[ALTLocalizedTitleErrorKey] = title
|
||||
|
||||
let error = ALTWrappedError(error: self, userInfo: userInfo)
|
||||
return error
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizedForSerialization() -> NSError
|
||||
{
|
||||
var userInfo = self.userInfo
|
||||
userInfo[NSLocalizedFailureErrorKey] = self.localizedFailure
|
||||
userInfo[NSLocalizedDescriptionKey] = self.localizedDescription
|
||||
userInfo[NSLocalizedFailureErrorKey] = self.localizedFailure
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = self.localizedFailureReason
|
||||
userInfo[NSLocalizedRecoverySuggestionErrorKey] = self.localizedRecoverySuggestion
|
||||
userInfo[NSDebugDescriptionErrorKey] = self.localizedDebugDescription
|
||||
|
||||
// Remove userInfo values that don't conform to NSSecureEncoding.
|
||||
userInfo = userInfo.filter { (key, value) in
|
||||
@@ -75,22 +91,160 @@ extension NSError
|
||||
// Sanitize underlying errors.
|
||||
if let underlyingError = userInfo[NSUnderlyingErrorKey] as? Error
|
||||
{
|
||||
let sanitizedError = (underlyingError as NSError).sanitizedForCoreData()
|
||||
let sanitizedError = (underlyingError as NSError).sanitizedForSerialization()
|
||||
userInfo[NSUnderlyingErrorKey] = sanitizedError
|
||||
}
|
||||
|
||||
if #available(iOS 14.5, macOS 11.3, *), let underlyingErrors = userInfo[NSMultipleUnderlyingErrorsKey] as? [Error]
|
||||
{
|
||||
let sanitizedErrors = underlyingErrors.map { ($0 as NSError).sanitizedForCoreData() }
|
||||
let sanitizedErrors = underlyingErrors.map { ($0 as NSError).sanitizedForSerialization() }
|
||||
userInfo[NSMultipleUnderlyingErrorsKey] = sanitizedErrors
|
||||
}
|
||||
|
||||
let error = NSError(domain: self.domain, code: self.code, userInfo: userInfo)
|
||||
return error
|
||||
}
|
||||
|
||||
func formattedDetailedDescription(with font: ALTFont) -> NSAttributedString
|
||||
{
|
||||
#if canImport(UIKit)
|
||||
let boldFontDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) ?? font.fontDescriptor
|
||||
let boldFont = ALTFont(descriptor: boldFontDescriptor, size: font.pointSize)
|
||||
#else
|
||||
let boldFontDescriptor = font.fontDescriptor.withSymbolicTraits(.bold)
|
||||
let boldFont = ALTFont(descriptor: boldFontDescriptor, size: font.pointSize) ?? font
|
||||
#endif
|
||||
|
||||
var preferredKeyOrder = [
|
||||
NSDebugDescriptionErrorKey,
|
||||
NSLocalizedDescriptionKey,
|
||||
NSLocalizedFailureErrorKey,
|
||||
NSLocalizedFailureReasonErrorKey,
|
||||
NSLocalizedRecoverySuggestionErrorKey,
|
||||
ALTLocalizedTitleErrorKey,
|
||||
ALTSourceFileErrorKey,
|
||||
ALTSourceLineErrorKey,
|
||||
NSUnderlyingErrorKey
|
||||
]
|
||||
|
||||
if #available(iOS 14.5, macOS 11.3, *)
|
||||
{
|
||||
preferredKeyOrder.append(NSMultipleUnderlyingErrorsKey)
|
||||
}
|
||||
|
||||
var userInfo = self.userInfo
|
||||
userInfo[NSDebugDescriptionErrorKey] = self.localizedDebugDescription
|
||||
userInfo[NSLocalizedDescriptionKey] = self.localizedDescription
|
||||
userInfo[NSLocalizedFailureErrorKey] = self.localizedFailure
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = self.localizedFailureReason
|
||||
userInfo[NSLocalizedRecoverySuggestionErrorKey] = self.localizedRecoverySuggestion
|
||||
|
||||
let sortedUserInfo = userInfo.sorted { (a, b) in
|
||||
let indexA = preferredKeyOrder.firstIndex(of: a.key)
|
||||
let indexB = preferredKeyOrder.firstIndex(of: b.key)
|
||||
|
||||
switch (indexA, indexB)
|
||||
{
|
||||
case (let indexA?, let indexB?): return indexA < indexB
|
||||
case (_?, nil): return true // indexA exists, indexB is nil, so A should come first.
|
||||
case (nil, _?): return false // indexA is nil, indexB exists, so B should come first.
|
||||
case (nil, nil): return a.key < b.key // both indexes are nil, so sort alphabetically.
|
||||
}
|
||||
}
|
||||
|
||||
let detailedDescription = NSMutableAttributedString()
|
||||
|
||||
for (key, value) in sortedUserInfo
|
||||
{
|
||||
let keyName: String
|
||||
switch key
|
||||
{
|
||||
case NSDebugDescriptionErrorKey: keyName = NSLocalizedString("Debug Description", comment: "")
|
||||
case NSLocalizedDescriptionKey: keyName = NSLocalizedString("Error Description", comment: "")
|
||||
case NSLocalizedFailureErrorKey: keyName = NSLocalizedString("Failure", comment: "")
|
||||
case NSLocalizedFailureReasonErrorKey: keyName = NSLocalizedString("Failure Reason", comment: "")
|
||||
case NSLocalizedRecoverySuggestionErrorKey: keyName = NSLocalizedString("Recovery Suggestion", comment: "")
|
||||
case ALTLocalizedTitleErrorKey: keyName = NSLocalizedString("Title", comment: "")
|
||||
case ALTSourceFileErrorKey: keyName = NSLocalizedString("Source File", comment: "")
|
||||
case ALTSourceLineErrorKey: keyName = NSLocalizedString("Source Line", comment: "")
|
||||
case NSUnderlyingErrorKey: keyName = NSLocalizedString("Underlying Error", comment: "")
|
||||
default:
|
||||
if #available(iOS 14.5, macOS 11.3, *), key == NSMultipleUnderlyingErrorsKey
|
||||
{
|
||||
keyName = NSLocalizedString("Underlying Errors", comment: "")
|
||||
}
|
||||
else
|
||||
{
|
||||
keyName = key
|
||||
}
|
||||
}
|
||||
|
||||
let attributedKey = NSAttributedString(string: keyName, attributes: [.font: boldFont])
|
||||
let attributedValue = NSAttributedString(string: String(describing: value), attributes: [.font: font])
|
||||
|
||||
let attributedString = NSMutableAttributedString(attributedString: attributedKey)
|
||||
attributedString.mutableString.append("\n")
|
||||
attributedString.append(attributedValue)
|
||||
|
||||
if !detailedDescription.string.isEmpty
|
||||
{
|
||||
detailedDescription.mutableString.append("\n\n")
|
||||
}
|
||||
|
||||
detailedDescription.append(attributedString)
|
||||
}
|
||||
|
||||
// Support dark mode
|
||||
#if canImport(UIKit)
|
||||
if #available(iOS 13, *)
|
||||
{
|
||||
detailedDescription.addAttribute(.foregroundColor, value: UIColor.label, range: NSMakeRange(0, detailedDescription.length))
|
||||
}
|
||||
#else
|
||||
detailedDescription.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSMakeRange(0, detailedDescription.length))
|
||||
#endif
|
||||
|
||||
return detailedDescription
|
||||
}
|
||||
}
|
||||
|
||||
extension Error
|
||||
public extension NSError
|
||||
{
|
||||
typealias UserInfoProvider = (Error, String) -> Any?
|
||||
|
||||
@objc
|
||||
class func alt_setUserInfoValueProvider(forDomain domain: String, provider: UserInfoProvider?)
|
||||
{
|
||||
NSError.setUserInfoValueProvider(forDomain: domain) { (error, key) in
|
||||
let nsError = error as NSError
|
||||
|
||||
switch key
|
||||
{
|
||||
case NSLocalizedDescriptionKey:
|
||||
if nsError.localizedFailure != nil
|
||||
{
|
||||
// Error has localizedFailure, so return nil to construct localizedDescription from it + localizedFailureReason.
|
||||
return nil
|
||||
}
|
||||
else if let localizedDescription = provider?(error, NSLocalizedDescriptionKey) as? String
|
||||
{
|
||||
// Only call provider() if there is no localizedFailure.
|
||||
return localizedDescription
|
||||
}
|
||||
|
||||
// Otherwise, return failureReason for localizedDescription to avoid system prepending "Operation Failed" message.
|
||||
// Do NOT return provider(NSLocalizedFailureReason), which might be unexpectedly nil if unrecognized error code.
|
||||
return nsError.localizedFailureReason
|
||||
|
||||
default:
|
||||
let value = provider?(error, key)
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension Error
|
||||
{
|
||||
var underlyingError: Error? {
|
||||
let underlyingError = (self as NSError).userInfo[NSUnderlyingErrorKey] as? Error
|
||||
@@ -98,46 +252,22 @@ extension Error
|
||||
}
|
||||
|
||||
var localizedErrorCode: String {
|
||||
let localizedErrorCode = String(format: NSLocalizedString("%@ error %@", comment: ""), (self as NSError).domain, (self as NSError).code as NSNumber)
|
||||
let nsError = self as NSError
|
||||
let localizedErrorCode = String(format: NSLocalizedString("%@ %@", comment: ""), nsError.domain, self.displayCode as NSNumber)
|
||||
return localizedErrorCode
|
||||
}
|
||||
}
|
||||
|
||||
protocol ALTLocalizedError: LocalizedError, CustomNSError
|
||||
{
|
||||
var failure: String? { get }
|
||||
|
||||
var underlyingError: Error? { get }
|
||||
}
|
||||
|
||||
extension ALTLocalizedError
|
||||
{
|
||||
var errorUserInfo: [String : Any] {
|
||||
let userInfo = ([
|
||||
NSLocalizedDescriptionKey: self.errorDescription,
|
||||
NSLocalizedFailureReasonErrorKey: self.failureReason,
|
||||
NSLocalizedFailureErrorKey: self.failure,
|
||||
NSUnderlyingErrorKey: self.underlyingError
|
||||
] as [String: Any?]).compactMapValues { $0 }
|
||||
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 }
|
||||
var displayCode: Int {
|
||||
guard let serverError = self as? ALTServerError else {
|
||||
// Not ALTServerError, so display regular code.
|
||||
return (self as NSError).code
|
||||
}
|
||||
|
||||
let errorDescription = errorFailure + " " + failureReason
|
||||
return errorDescription
|
||||
// We want ALTServerError codes to start at 2000,
|
||||
// but we can't change them without breaking AltServer compatibility.
|
||||
// Instead, we just add 2000 when displaying code to user
|
||||
// to make it appear as if codes start at 2000 normally.
|
||||
let code = 2000 + serverError.code.rawValue
|
||||
return code
|
||||
}
|
||||
|
||||
var failureReason: String? { (self.underlyingError as NSError?)?.localizedDescription }
|
||||
var recoverySuggestion: String? { (self.underlyingError as NSError?)?.localizedRecoverySuggestion }
|
||||
var helpAnchor: String? { (self.underlyingError as NSError?)?.helpAnchor }
|
||||
}
|
||||
|
||||
249
Shared/Server Protocol/CodableError.swift
Normal file
249
Shared/Server Protocol/CodableError.swift
Normal file
@@ -0,0 +1,249 @@
|
||||
//
|
||||
// CodableError.swift
|
||||
// AltKit
|
||||
//
|
||||
// Created by Riley Testut on 3/5/20.
|
||||
// Copyright © 2020 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// Can only automatically conform ALTServerError.Code to Codable, not ALTServerError itself
|
||||
extension ALTServerError.Code: Codable {}
|
||||
|
||||
private extension ErrorUserInfoKey
|
||||
{
|
||||
static let altLocalizedDescription: String = "ALTLocalizedDescription"
|
||||
static let altLocalizedFailureReason: String = "ALTLocalizedFailureReason"
|
||||
static let altLocalizedRecoverySuggestion: String = "ALTLocalizedRecoverySuggestion"
|
||||
static let altDebugDescription: String = "ALTDebugDescription"
|
||||
}
|
||||
|
||||
extension CodableError
|
||||
{
|
||||
enum UserInfoValue: Codable
|
||||
{
|
||||
case unknown
|
||||
case string(String)
|
||||
case number(Int)
|
||||
case error(NSError)
|
||||
case codableError(CodableError)
|
||||
indirect case array([UserInfoValue])
|
||||
indirect case dictionary([String: UserInfoValue])
|
||||
|
||||
var value: Any? {
|
||||
switch self
|
||||
{
|
||||
case .unknown: return nil
|
||||
case .string(let string): return string
|
||||
case .number(let number): return number
|
||||
case .error(let error): return error
|
||||
case .codableError(let error): return error.error
|
||||
case .array(let array): return array.compactMap { $0.value } // .compactMap instead of .map to ensure nil values are removed.
|
||||
case .dictionary(let dictionary): return dictionary.compactMapValues { $0.value } // .compactMapValues instead of .mapValues to ensure nil values are removed.
|
||||
}
|
||||
}
|
||||
|
||||
var codableValue: Codable? {
|
||||
switch self
|
||||
{
|
||||
case .unknown, .string, .number: return self.value as? Codable
|
||||
case .codableError(let error): return error
|
||||
case .error(let nsError):
|
||||
// Ignore error because we don't want to fail completely if error contains invalid user info value.
|
||||
let sanitizedError = nsError.sanitizedForSerialization()
|
||||
let data = try? NSKeyedArchiver.archivedData(withRootObject: sanitizedError, requiringSecureCoding: true)
|
||||
return data
|
||||
|
||||
case .array(let array): return array
|
||||
case .dictionary(let dictionary): return dictionary
|
||||
}
|
||||
}
|
||||
|
||||
init(_ rawValue: Any?)
|
||||
{
|
||||
switch rawValue
|
||||
{
|
||||
case let string as String: self = .string(string)
|
||||
case let number as Int: self = .number(number)
|
||||
case let error as NSError: self = .codableError(CodableError(error: error))
|
||||
case let array as [Any]: self = .array(array.compactMap(UserInfoValue.init))
|
||||
case let dictionary as [String: Any]: self = .dictionary(dictionary.compactMapValues(UserInfoValue.init))
|
||||
default: self = .unknown
|
||||
}
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws
|
||||
{
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
if
|
||||
let data = try? container.decode(Data.self),
|
||||
let error = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSError.self, from: data)
|
||||
{
|
||||
self = .error(error)
|
||||
}
|
||||
else if let codableError = try? container.decode(CodableError.self)
|
||||
{
|
||||
self = .codableError(codableError)
|
||||
}
|
||||
else if let string = try? container.decode(String.self)
|
||||
{
|
||||
self = .string(string)
|
||||
}
|
||||
else if let number = try? container.decode(Int.self)
|
||||
{
|
||||
self = .number(number)
|
||||
}
|
||||
else if let array = try? container.decode([UserInfoValue].self)
|
||||
{
|
||||
self = .array(array)
|
||||
}
|
||||
else if let dictionary = try? container.decode([String: UserInfoValue].self)
|
||||
{
|
||||
self = .dictionary(dictionary)
|
||||
}
|
||||
else
|
||||
{
|
||||
self = .unknown
|
||||
}
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws
|
||||
{
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
if let value = self.codableValue
|
||||
{
|
||||
try container.encode(value)
|
||||
}
|
||||
else
|
||||
{
|
||||
try container.encodeNil()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CodableError: Codable
|
||||
{
|
||||
var error: Error {
|
||||
return self.rawError ?? NSError(domain: self.errorDomain, code: self.errorCode, userInfo: self.userInfo ?? [:])
|
||||
}
|
||||
private var rawError: Error?
|
||||
|
||||
private var errorDomain: String
|
||||
private var errorCode: Int
|
||||
private var userInfo: [String: Any]?
|
||||
|
||||
private enum CodingKeys: String, CodingKey
|
||||
{
|
||||
case errorDomain
|
||||
case errorCode
|
||||
case legacyUserInfo = "userInfo"
|
||||
case errorUserInfo
|
||||
}
|
||||
|
||||
init(error: Error)
|
||||
{
|
||||
self.rawError = error
|
||||
|
||||
let nsError = error as NSError
|
||||
self.errorDomain = nsError.domain
|
||||
self.errorCode = nsError.code
|
||||
|
||||
if !nsError.userInfo.isEmpty
|
||||
{
|
||||
self.userInfo = nsError.userInfo
|
||||
}
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws
|
||||
{
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
// Assume ALTServerError.errorDomain if no explicit domain provided.
|
||||
self.errorDomain = try container.decodeIfPresent(String.self, forKey: .errorDomain) ?? ALTServerError.errorDomain
|
||||
self.errorCode = try container.decode(Int.self, forKey: .errorCode)
|
||||
|
||||
if let rawUserInfo = try container.decodeIfPresent([String: UserInfoValue].self, forKey: .errorUserInfo)
|
||||
{
|
||||
// Attempt decoding from .errorUserInfo first, because it will gracefully handle unknown user info values.
|
||||
|
||||
// Copy ALTLocalized... values to NSLocalized... if provider is nil or if error is unrecognized.
|
||||
// This ensures we preserve error messages if receiving an unknown error.
|
||||
var userInfo = rawUserInfo.compactMapValues { $0.value }
|
||||
|
||||
// Recognized == the provider returns value for NSLocalizedFailureReasonErrorKey, or error is ALTServerError.underlyingError.
|
||||
let provider = NSError.userInfoValueProvider(forDomain: self.errorDomain)
|
||||
let isRecognizedError = (
|
||||
provider?(self.error, NSLocalizedFailureReasonErrorKey) != nil ||
|
||||
(self.error._domain == ALTServerError.errorDomain && self.error._code == ALTServerError.underlyingError.rawValue)
|
||||
)
|
||||
|
||||
if !isRecognizedError
|
||||
{
|
||||
// Error not recognized, so copy over NSLocalizedDescriptionKey and NSLocalizedFailureReasonErrorKey.
|
||||
userInfo[NSLocalizedDescriptionKey] = userInfo[ErrorUserInfoKey.altLocalizedDescription]
|
||||
userInfo[NSLocalizedFailureReasonErrorKey] = userInfo[ErrorUserInfoKey.altLocalizedFailureReason]
|
||||
}
|
||||
|
||||
// Copy over NSLocalizedRecoverySuggestionErrorKey and NSDebugDescriptionErrorKey if provider returns nil.
|
||||
if provider?(self.error, NSLocalizedRecoverySuggestionErrorKey) == nil
|
||||
{
|
||||
userInfo[NSLocalizedRecoverySuggestionErrorKey] = userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion]
|
||||
}
|
||||
|
||||
if provider?(self.error, NSDebugDescriptionErrorKey) == nil
|
||||
{
|
||||
userInfo[NSDebugDescriptionErrorKey] = userInfo[ErrorUserInfoKey.altDebugDescription]
|
||||
}
|
||||
|
||||
userInfo[ErrorUserInfoKey.altLocalizedDescription] = nil
|
||||
userInfo[ErrorUserInfoKey.altLocalizedFailureReason] = nil
|
||||
userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion] = nil
|
||||
userInfo[ErrorUserInfoKey.altDebugDescription] = nil
|
||||
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
else if let rawUserInfo = try container.decodeIfPresent([String: UserInfoValue].self, forKey: .legacyUserInfo)
|
||||
{
|
||||
// Fall back to decoding .legacyUserInfo, which only supports String and NSError values.
|
||||
let userInfo = rawUserInfo.compactMapValues { $0.value }
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws
|
||||
{
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(self.errorDomain, forKey: .errorDomain)
|
||||
try container.encode(self.errorCode, forKey: .errorCode)
|
||||
|
||||
let rawLegacyUserInfo = self.userInfo?.compactMapValues { (value) -> UserInfoValue? in
|
||||
// .legacyUserInfo only supports String and NSError values.
|
||||
switch value
|
||||
{
|
||||
case let string as String: return .string(string)
|
||||
case let error as NSError: return .error(error) // Must use .error, not .codableError for backwards compatibility.
|
||||
default: return nil
|
||||
}
|
||||
}
|
||||
try container.encodeIfPresent(rawLegacyUserInfo, forKey: .legacyUserInfo)
|
||||
|
||||
let nsError = self.error as NSError
|
||||
|
||||
var userInfo = self.userInfo ?? [:]
|
||||
userInfo[ErrorUserInfoKey.altLocalizedDescription] = nsError.localizedDescription
|
||||
userInfo[ErrorUserInfoKey.altLocalizedFailureReason] = nsError.localizedFailureReason
|
||||
userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion] = nsError.localizedRecoverySuggestion
|
||||
userInfo[ErrorUserInfoKey.altDebugDescription] = nsError.localizedDebugDescription
|
||||
|
||||
// No need to use alternate key. This is a no-op if userInfo already contains localizedFailure,
|
||||
// but it caches the UserInfoProvider value if one exists.
|
||||
userInfo[NSLocalizedFailureErrorKey] = nsError.localizedFailure
|
||||
|
||||
let rawUserInfo = userInfo.compactMapValues { UserInfoValue($0) }
|
||||
try container.encodeIfPresent(rawUserInfo, forKey: .errorUserInfo)
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
//
|
||||
// CodableServerError.swift
|
||||
// AltKit
|
||||
//
|
||||
// Created by Riley Testut on 3/5/20.
|
||||
// Copyright © 2020 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// Can only automatically conform ALTServerError.Code to Codable, not ALTServerError itself
|
||||
extension ALTServerError.Code: Codable {}
|
||||
|
||||
extension CodableServerError
|
||||
{
|
||||
enum UserInfoValue: Codable
|
||||
{
|
||||
case string(String)
|
||||
case error(NSError)
|
||||
|
||||
public init(from decoder: Decoder) throws
|
||||
{
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
if
|
||||
let data = try? container.decode(Data.self),
|
||||
let error = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSError.self, from: data)
|
||||
{
|
||||
self = .error(error)
|
||||
}
|
||||
else if let string = try? container.decode(String.self)
|
||||
{
|
||||
self = .string(string)
|
||||
}
|
||||
else
|
||||
{
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "UserInfoValue value cannot be decoded")
|
||||
}
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws
|
||||
{
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
switch self
|
||||
{
|
||||
case .string(let string): try container.encode(string)
|
||||
case .error(let error):
|
||||
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: error, requiringSecureCoding: true) else {
|
||||
let context = EncodingError.Context(codingPath: container.codingPath, debugDescription: "UserInfoValue value \(self) cannot be encoded")
|
||||
throw EncodingError.invalidValue(self, context)
|
||||
}
|
||||
|
||||
try container.encode(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CodableServerError: Codable
|
||||
{
|
||||
var error: ALTServerError {
|
||||
return ALTServerError(self.errorCode, userInfo: self.userInfo ?? [:])
|
||||
}
|
||||
|
||||
private var errorCode: ALTServerError.Code
|
||||
private var userInfo: [String: Any]?
|
||||
|
||||
private enum CodingKeys: String, CodingKey
|
||||
{
|
||||
case errorCode
|
||||
case userInfo
|
||||
}
|
||||
|
||||
init(error: ALTServerError)
|
||||
{
|
||||
self.errorCode = error.code
|
||||
|
||||
var userInfo = error.userInfo
|
||||
if let localizedRecoverySuggestion = (error as NSError).localizedRecoverySuggestion
|
||||
{
|
||||
userInfo[NSLocalizedRecoverySuggestionErrorKey] = localizedRecoverySuggestion
|
||||
}
|
||||
|
||||
if !userInfo.isEmpty
|
||||
{
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws
|
||||
{
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
let errorCode = try container.decode(Int.self, forKey: .errorCode)
|
||||
self.errorCode = ALTServerError.Code(rawValue: errorCode) ?? .unknown
|
||||
|
||||
let rawUserInfo = try container.decodeIfPresent([String: UserInfoValue].self, forKey: .userInfo)
|
||||
|
||||
let userInfo = rawUserInfo?.mapValues { (value) -> Any in
|
||||
switch value
|
||||
{
|
||||
case .string(let string): return string
|
||||
case .error(let error): return error
|
||||
}
|
||||
}
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws
|
||||
{
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(self.error.code.rawValue, forKey: .errorCode)
|
||||
|
||||
let rawUserInfo = self.userInfo?.compactMapValues { (value) -> UserInfoValue? in
|
||||
switch value
|
||||
{
|
||||
case let string as String: return .string(string)
|
||||
case let error as NSError: return .error(error)
|
||||
default: return nil
|
||||
}
|
||||
}
|
||||
try container.encodeIfPresent(rawUserInfo, forKey: .userInfo)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,20 +197,21 @@ public enum ServerResponse: Decodable
|
||||
// from easily changing response format for a request in the future.
|
||||
public struct ErrorResponse: ServerMessageProtocol
|
||||
{
|
||||
public var version = 2
|
||||
public var version = 3
|
||||
public var identifier = "ErrorResponse"
|
||||
|
||||
public var error: ALTServerError {
|
||||
return self.serverError?.error ?? ALTServerError(self.errorCode)
|
||||
// Must be ALTServerError
|
||||
return self.serverError.map { ALTServerError($0.error) } ?? ALTServerError(self.errorCode)
|
||||
}
|
||||
private var serverError: CodableServerError?
|
||||
private var serverError: CodableError?
|
||||
|
||||
// Legacy (v1)
|
||||
private var errorCode: ALTServerError.Code
|
||||
|
||||
public init(error: ALTServerError)
|
||||
{
|
||||
self.serverError = CodableServerError(error: error)
|
||||
self.serverError = CodableError(error: error)
|
||||
self.errorCode = error.code
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user