merge AltStore 1.6.3, add dynamic anisette lists, merge SideJITServer integration

* Change error from Swift.Error to NSError

* Adds ResultOperation.localizedFailure

* Finish Riley's monster commit

3b38d725d7
May the Gods have mercy on my soul.

* Fix format strings I broke

* Include "Enable JIT" errors in Error Log

* Fix minimuxer status checking

* [skip ci] Update the no wifi message to include VPN

* Opens Error Log when tapping ToastView

* Fixes Error Log context menu covering cell content

* Fixes Error Log context menu appearing while scrolling

* Fixes incorrect Search FAQ URL

* Fix Error Log showing UIAlertController on iOS 14+

* Fix Error Log not showing UIAlertController on iOS <=13

* Fix wrong color in AuthenticationViewController

* Fix typo

* Fixes logging non-AltServerErrors as AltServerError.underlyingError

* Limits quitting other AltStore/SideStore processes to database migrations

* Skips logging cancelled errors

* Replaces StoreApp.latestVersion with latestSupportedVersion + latestAvailableVersion

We now store the latest supported version as a relationship on StoreApp, rather than the latest available version. This allows us to reference the latest supported version in predicates and sort descriptors.

However, we kept the underlying Core Data property name the same to avoid extra migration.

* Conforms OperatingSystemVersion to Comparable

* Parses AppVersion.minOSVersion/maxOSVersion from source JSON

* Supports non-NSManagedObjects for @Managed properties

This allows us to use @Managed with properties that may or may not be NSManagedObjects at runtime (e.g. protocols). If they are, Managed will keep strong reference to context like before.

* Supports optional @Managed properties

* Conforms AppVersion to AppProtocol

* Verifies min/max OS version before downloading app + asks user to download older app version if necessary

* Improves error message when file does not exist at AppVersion.downloadURL

* Removes unnecessary StoreApp convenience properties

* Removes unnecessary StoreApp convenience properties as well as fix other issues

* Remove Settings bundle, add SwiftUI view instead

Fix refresh all shortcut intent

* Update AuthenticationOperation.swift

Signed-off-by: June Park <rjp2030@outlook.com>

* Fix build issues given by develop

* Add availability check to fix CI build(?)

* If it's gonna be that way...

---------

Signed-off-by: June Park <rjp2030@outlook.com>
Co-authored-by: nythepegasus <nythepegasus84@gmail.com>
Co-authored-by: Riley Testut <riley@rileytestut.com>
Co-authored-by: ny <me@nythepegas.us>
This commit is contained in:
June Park
2024-08-06 10:43:52 +09:00
committed by GitHub
parent 83ece72ae1
commit 1713fccfc4
60 changed files with 2170 additions and 1067 deletions

View File

@@ -8,9 +8,16 @@
#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 = @"AltServer.InstallationError";
NSErrorDomain const AltServerConnectionErrorDomain = @"AltServer.ConnectionError";
NSErrorUserInfoKey const ALTUnderlyingErrorDomainErrorKey = @"underlyingErrorDomain";
NSErrorUserInfoKey const ALTUnderlyingErrorCodeErrorKey = @"underlyingErrorCode";
@@ -24,8 +31,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 +56,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 +70,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 SideStore.", @"");
#endif
}
return nil;
}
default:
return nil;
}
}
- (nullable NSString *)altserver_localizedFailureReason
{
switch ((ALTServerError)self.code)
@@ -80,12 +142,21 @@ 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 SideStore.", @"");
#endif
}
case ALTServerErrorLostConnection:
return NSLocalizedString(@"Lost connection to SideStore.", @"");
@@ -93,8 +164,8 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
return NSLocalizedString(@"SideStore could not find this device.", @"");
case ALTServerErrorDeviceWriteFailed:
return NSLocalizedString(@"Failed to write app data to device.", @"");
return NSLocalizedString(@"SideStore could not write data to this device.", @"");
case ALTServerErrorInvalidRequest:
return NSLocalizedString(@"SideStore received an invalid request.", @"");
@@ -102,14 +173,20 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
return NSLocalizedString(@"SideStore sent an invalid response.", @"");
case ALTServerErrorInvalidApp:
return NSLocalizedString(@"The app is invalid.", @"");
return NSLocalizedString(@"The app is in an invalid format.", @"");
case ALTServerErrorInstallationFailed:
{
NSError *underlyingError = self.userInfo[NSUnderlyingErrorKey];
if (underlyingError != nil) {
return underlyingError.localizedFailureReason ?: underlyingError.localizedDescription;
}
return NSLocalizedString(@"An error occured 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 SideStore.", @"");
@@ -117,8 +194,8 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
return NSLocalizedString(@"SideStore does not support this request.", @"");
case ALTServerErrorUnknownResponse:
return NSLocalizedString(@"Received an unknown response from SideStore.", @"");
return NSLocalizedString(@"SideStore received an unknown response from SideStore.", @"");
case ALTServerErrorInvalidAnisetteData:
return NSLocalizedString(@"The provided anisette data is invalid.", @"");
@@ -153,7 +230,19 @@ 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 found, fall through to AltServerErrorDeviceNotFound
}
case ALTServerErrorDeviceNotFound:
return NSLocalizedString(@"Make sure you have trusted this device with your computer and Wi-Fi sync is enabled.", @"");
@@ -182,6 +271,13 @@ 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 +287,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 +328,7 @@ NSErrorUserInfoKey const ALTOperatingSystemVersionErrorKey = @"ALTOperatingSyste
#pragma mark - AltServerConnectionErrorDomain -
- (nullable NSString *)altserver_connection_localizedDescription
- (nullable NSString *)altserver_connection_localizedFailureReason
{
switch ((ALTServerConnectionError)self.code)
{

View 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, // TODO: Figure out where these come from
// 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
//}

View 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

View 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

View File

@@ -22,12 +22,8 @@ public extension ALTServerError
case is EncodingError: self = ALTServerError(.invalidResponse, underlyingError: error)
case let error as NSError:
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)
}
}

View File

@@ -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? {
@@ -21,52 +31,52 @@ extension NSError
let debugDescription = (self.userInfo[NSDebugDescriptionErrorKey] as? String) ?? (NSError.userInfoValueProvider(forDomain: self.domain)?(self, NSDebugDescriptionErrorKey) as? String)
return debugDescription
}
@objc(alt_localizedTitle)
var localizedTitle: String? {
return self.userInfo[ALTLocalizedTitleErrorKey] as? String
}
@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[NSLocalizedFailureReasonErrorKey] = failure
return ALTWrappedError(error: self, userInfo: userInfo)
}
else if self.localizedFailure == nil && self.localizedFailureReason == nil && self.localizedDescription.contains(self.localizedErrorCode)
}
@objc(alt_errorWithLocalizedTitle:)
func withLocalizedTitle(_ title: String) -> NSError {
switch self
{
// Default localizedDescription, so replace with just the localized error code portion.
userInfo[NSLocalizedFailureReasonErrorKey] = "(\(self.localizedErrorCode).)"
case var error as any ALTLocalizedError:
error.errorTitle = title
return error as NSError
default:
var userInfo = self.userInfo
userInfo[ALTLocalizedTitleErrorKey] = title
return ALTWrappedError(error: self, userInfo: userInfo)
}
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
func sanitizedForSerialization() -> NSError
{
var userInfo = self.userInfo
userInfo[NSLocalizedFailureErrorKey] = self.localizedFailure
userInfo[NSLocalizedDescriptionKey] = self.localizedDescription
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
return (value as AnyObject) is NSSecureCoding
@@ -75,69 +85,165 @@ 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
#else
let boldFontDescriptor = font.fontDescriptor.withSymbolicTraits(.bold)
#endif
let boldFont = ALTFont(descriptor: boldFontDescriptor, size: font.pointSize) ?? font
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[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, indexA should come first
case (nil, _?): return false // indexB exists, indexB is nil, indexB should come first
case (nil, nil): return a.key < b.key // both 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:
return provider?(error, key)
}
}
}
}
public extension Error
{
var underlyingError: Error? {
let underlyingError = (self as NSError).userInfo[NSUnderlyingErrorKey] as? Error
return underlyingError
return (self as NSError).userInfo[NSUnderlyingErrorKey] as? Error
}
var localizedErrorCode: String {
let localizedErrorCode = String(format: NSLocalizedString("%@ error %@", comment: ""), (self as NSError).domain, (self as NSError).code as NSNumber)
return localizedErrorCode
return String(format: NSLocalizedString("%@ %@", comment: ""), (self as NSError).domain, self.displayCode as NSNumber)
}
}
protocol ALTLocalizedError: LocalizedError, CustomNSError
{
var failure: String? { get }
var underlyingError: Error? { get }
}
var displayCode: Int {
guard let serverError = self as? ALTServerError else {
return (self as NSError).code
}
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
/* 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 the user to make it appear as if the codes start at 2000 anyway.
*/
return 2000 + serverError.code.rawValue
}
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?)?.localizedDescription }
var recoverySuggestion: String? { (self.underlyingError as NSError?)?.localizedRecoverySuggestion }
var helpAnchor: String? { (self.underlyingError as NSError?)?.helpAnchor }
}

View 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)
}
}

View File

@@ -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)
}
}

View File

@@ -197,20 +197,20 @@ 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)
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
}
}