|
| 1 | +// |
| 2 | +// ALTServerConnectionError.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Joseph Mattiello on 2/24/23. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public enum ALTServerConnectionError: Int, LocalizedError { |
| 11 | + case unknown |
| 12 | + case deviceLocked |
| 13 | + case invalidRequest |
| 14 | + case invalidResponse |
| 15 | + case usbmuxd |
| 16 | + case ssl |
| 17 | + case timedOut |
| 18 | + |
| 19 | + public static var errorDomain: String { return "com.rileytestut.AltServer.Connection" } |
| 20 | +} |
| 21 | + |
| 22 | +public extension ALTServerConnectionError { |
| 23 | + var errorDescription: String? { |
| 24 | + switch self { |
| 25 | + case .unknown: |
| 26 | + return NSLocalizedString("Unknown connection error", comment: "ALTServerConnectionError") |
| 27 | + case .deviceLocked: |
| 28 | + return NSLocalizedString("Device locked", comment: "ALTServerConnectionError") |
| 29 | + case .invalidRequest: |
| 30 | + return NSLocalizedString("Invalid request", comment: "ALTServerConnectionError") |
| 31 | + case .invalidResponse: |
| 32 | + return NSLocalizedString("Invalid response", comment: "ALTServerConnectionError") |
| 33 | + case .usbmuxd: |
| 34 | + return NSLocalizedString("USBMuxd error", comment: "ALTServerConnectionError") |
| 35 | + case .ssl: |
| 36 | + return NSLocalizedString("SSL error", comment: "ALTServerConnectionError") |
| 37 | + case .timedOut: |
| 38 | + return NSLocalizedString("Timed out", comment: "ALTServerConnectionError") |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +public extension ALTServerConnectionError { |
| 44 | + var recoverySuggestion: String? { |
| 45 | + switch self { |
| 46 | + case .deviceLocked: |
| 47 | + return NSLocalizedString("Unlock your device and try again.", comment: "ALTServerConnectionError recovery suggestion") |
| 48 | + case .invalidRequest: |
| 49 | + return NSLocalizedString("Make sure AltServer is running and try again.", comment: "ALTServerConnectionError recovery suggestion") |
| 50 | + case .invalidResponse: |
| 51 | + return NSLocalizedString("Make sure AltServer is running and try again.", comment: "ALTServerConnectionError recovery suggestion") |
| 52 | + case .usbmuxd: |
| 53 | + return NSLocalizedString("Make sure iTunes is not running, and no other software is using the device over USB.", comment: "ALTServerConnectionError recovery suggestion") |
| 54 | + case .ssl: |
| 55 | + return NSLocalizedString("Make sure your computer's time and date are correct, and try again.", comment: "ALTServerConnectionError recovery suggestion") |
| 56 | + case .timedOut: |
| 57 | + return NSLocalizedString("Make sure your device is unlocked and try again.", comment: "ALTServerConnectionError recovery suggestion") |
| 58 | + default: |
| 59 | + return nil |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public extension ALTServerConnectionError { |
| 65 | + var failureReason: String? { |
| 66 | + switch self { |
| 67 | + case .deviceLocked: |
| 68 | + return NSLocalizedString("Your device is locked. Unlock your device and try again.", comment: "ALTServerConnectionError failure reason") |
| 69 | + case .invalidRequest: |
| 70 | + return NSLocalizedString("The request sent to AltServer is invalid.", comment: "ALTServerConnectionError failure reason") |
| 71 | + case .invalidResponse: |
| 72 | + return NSLocalizedString("The response from AltServer is invalid.", comment: "ALTServerConnectionError failure reason") |
| 73 | + case .usbmuxd: |
| 74 | + return NSLocalizedString("AltServer cannot communicate with your device over USB. Make sure iTunes is not running, and no other software is using the device over USB.", comment: "ALTServerConnectionError failure reason") |
| 75 | + case .ssl: |
| 76 | + return NSLocalizedString("There is a problem with the SSL connection to AltServer. Make sure your computer's time and date are correct, and try again.", comment: "ALTServerConnectionError failure reason") |
| 77 | + case .timedOut: |
| 78 | + return NSLocalizedString("The connection to AltServer timed out. Make sure your device is unlocked and try again.", comment: "ALTServerConnectionError failure reason") |
| 79 | + case .unknown: |
| 80 | + return NSLocalizedString("An unknown error occurred with AltServer.", comment: "ALTServerConnectionError failure reason") |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +public extension ALTServerConnectionError { |
| 86 | + var code: Int { |
| 87 | + return self.rawValue |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +extension ALTServerConnectionError: Codable { |
| 93 | + enum CodingKeys: String, CodingKey { |
| 94 | + case deviceLocked |
| 95 | + case invalidRequest |
| 96 | + case invalidResponse |
| 97 | + case usbmuxd |
| 98 | + case ssl |
| 99 | + case timedOut |
| 100 | + case unknown |
| 101 | + } |
| 102 | + |
| 103 | + public init(from decoder: Decoder) throws { |
| 104 | + let values = try decoder.container(keyedBy: CodingKeys.self) |
| 105 | + |
| 106 | + if let _ = try? values.decode(Bool.self, forKey: .deviceLocked) { |
| 107 | + self = .deviceLocked |
| 108 | + return |
| 109 | + } |
| 110 | + if let _ = try? values.decode(Bool.self, forKey: .invalidRequest) { |
| 111 | + self = .invalidRequest |
| 112 | + return |
| 113 | + } |
| 114 | + if let _ = try? values.decode(Bool.self, forKey: .invalidResponse) { |
| 115 | + self = .invalidResponse |
| 116 | + return |
| 117 | + } |
| 118 | + if let _ = try? values.decode(Bool.self, forKey: .usbmuxd) { |
| 119 | + self = .usbmuxd |
| 120 | + return |
| 121 | + } |
| 122 | + if let _ = try? values.decode(Bool.self, forKey: .ssl) { |
| 123 | + self = .ssl |
| 124 | + return |
| 125 | + } |
| 126 | + if let _ = try? values.decode(Bool.self, forKey: .timedOut) { |
| 127 | + self = .timedOut |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + self = .unknown |
| 132 | + } |
| 133 | + |
| 134 | + public func encode(to encoder: Encoder) throws { |
| 135 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 136 | + |
| 137 | + switch self { |
| 138 | + case .deviceLocked: |
| 139 | + try container.encode(true, forKey: .deviceLocked) |
| 140 | + case .invalidRequest: |
| 141 | + try container.encode(true, forKey: .invalidRequest) |
| 142 | + case .invalidResponse: |
| 143 | + try container.encode(true, forKey: .invalidResponse) |
| 144 | + case .usbmuxd: |
| 145 | + try container.encode(true, forKey: .usbmuxd) |
| 146 | + case .ssl: |
| 147 | + try container.encode(true, forKey: .ssl) |
| 148 | + case .timedOut: |
| 149 | + try container.encode(true, forKey: .timedOut) |
| 150 | + case .unknown: |
| 151 | + break |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments