diff --git a/AltBackup/BackupController.swift b/AltBackup/BackupController.swift old mode 100644 new mode 100755 index 93435e9c..98988d50 --- a/AltBackup/BackupController.swift +++ b/AltBackup/BackupController.swift @@ -26,37 +26,65 @@ extension Error struct BackupError: ALTLocalizedError { - enum Code + enum Code: ALTErrorEnum, RawRepresentable { case invalidBundleID case appGroupNotFound(String?) case randomError // Used for debugging. - } - - let code: Code - - let sourceFile: String - let sourceFileLine: Int - - var failure: String? - - var failureReason: String? { - switch self.code - { - case .invalidBundleID: return NSLocalizedString("The bundle identifier is invalid.", comment: "") - case .appGroupNotFound(let appGroup): - if let appGroup = appGroup - { - return String(format: NSLocalizedString("The app group “%@” could not be found.", comment: ""), appGroup) + + // Provide failure reason for each error code + var errorFailureReason: String { + switch self { + case .invalidBundleID: + return NSLocalizedString("The bundle identifier is invalid.", comment: "") + case .appGroupNotFound(let appGroup): + if let appGroup = appGroup { + return String(format: NSLocalizedString("The app group “%@” could not be found.", comment: ""), appGroup) + } else { + return NSLocalizedString("The AltStore app group could not be found.", comment: "") + } + case .randomError: + return NSLocalizedString("A random error occurred.", comment: "") } - else - { - return NSLocalizedString("The AltStore app group could not be found.", comment: "") + } + + static var errorDomain: String { + return "com.sidestore.BackupError" + } + + // Add a raw value for RawRepresentable conformance + var rawValue: Int { + switch self { + case .invalidBundleID: return 0 + case .appGroupNotFound: return 1 + case .randomError: return 2 + } + } + + // Initializer for RawRepresentable + init?(rawValue: Int) { + switch rawValue { + case 0: self = .invalidBundleID + case 1: self = .appGroupNotFound(nil) + case 2: self = .randomError + default: return nil } - case .randomError: return NSLocalizedString("A random error occured.", comment: "") } } + let code: Code + let sourceFile: String + let sourceFileLine: Int + var failure: String? + + var errorTitle: String? + var errorFailure: String? + + var failureReason: String? { + return self.code.errorFailureReason + } + + // Conforming to the userInfo required by ALTLocalizedError var errorUserInfo: [String : Any] { let userInfo: [String: Any?] = [NSLocalizedDescriptionKey: self.errorDescription, NSLocalizedFailureReasonErrorKey: self.failureReason, @@ -65,6 +93,11 @@ struct BackupError: ALTLocalizedError ErrorUserInfoKey.sourceFileLine: self.sourceFileLine] return userInfo.compactMapValues { $0 } } + + // Implement description for CustomStringConvertible + var description: String { + return "\(errorTitle ?? "Unknown Error"): \(failureReason ?? "No reason available")" + } init(_ code: Code, description: String? = nil, file: String = #file, line: Int = #line) { @@ -72,6 +105,8 @@ struct BackupError: ALTLocalizedError self.failure = description self.sourceFile = file self.sourceFileLine = line + self.errorTitle = NSLocalizedString("Backup Error", comment: "") + self.errorFailure = description } } diff --git a/Shared/Errors/ALTLocalizedError.swift b/Shared/Errors/ALTLocalizedError.swift old mode 100644 new mode 100755 index 53728288..9a486741 --- a/Shared/Errors/ALTLocalizedError.swift +++ b/Shared/Errors/ALTLocalizedError.swift @@ -58,8 +58,8 @@ 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. + guard (self as NSError).localizedFailureReason == nil else { + // Error has localizedFailureReason, so return nil to construct localizedDescription from it + localizedFailureReason. return nil } @@ -85,7 +85,7 @@ public extension ALTLocalizedError } var description: String { - let description = "\(self.localizedErrorCode) “\(self.localizedDescription)”" + let description = "\(self.errorCode) “\(self.localizedDescription)”" return description } }