diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2020-07-21 14:28:54 +0300 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2020-07-22 18:35:43 +0300 |
| commit | 5b49407021dfd6d2b8b9c971da1400ad9ecef574 (patch) | |
| tree | 6ffe1947030490148a5bdcc5e23879f09d361875 | |
| parent | 868f0bdf1d4bb05babbfeec817315027283cf2f1 (diff) | |
| download | mullvadvpn-5b49407021dfd6d2b8b9c971da1400ad9ecef574.tar.xz mullvadvpn-5b49407021dfd6d2b8b9c971da1400ad9ecef574.zip | |
Support pattern matching in ServerErrorResponse
| -rw-r--r-- | ios/MullvadVPN/MullvadRest.swift | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/ios/MullvadVPN/MullvadRest.swift b/ios/MullvadVPN/MullvadRest.swift index fd1b7d9c94..dc777f7ebd 100644 --- a/ios/MullvadVPN/MullvadRest.swift +++ b/ios/MullvadVPN/MullvadRest.swift @@ -22,22 +22,37 @@ enum HttpMethod: String { case delete = "DELETE" } -/// A known list of Rest API error codes -enum RestErrorCode: String { - case invalidAccount = "INVALID_ACCOUNT" - case keyLimitReached = "KEY_LIMIT_REACHED" -} - /// A struct that represents a server response in case of error (any HTTP status code except 2xx). -struct ServerErrorResponse: LocalizedError, Decodable, RestResponse { +struct ServerErrorResponse: LocalizedError, Decodable, RestResponse, Equatable { + /// A list of known server error codes + enum Code: String, Equatable { + case invalidAccount = "INVALID_ACCOUNT" + case keyLimitReached = "KEY_LIMIT_REACHED" + case pubKeyNotFound = "PUBKEY_NOT_FOUND" + + static func ~= (pattern: Self, value: ServerErrorResponse) -> Bool { + return pattern.rawValue == value.code + } + } + + static var invalidAccount: Code { + return .invalidAccount + } + static var keyLimitReached: Code { + return .keyLimitReached + } + static var pubKeyNotFound: Code { + return .pubKeyNotFound + } + let code: String let error: String? var errorDescription: String? { switch code { - case RestErrorCode.keyLimitReached.rawValue: + case Code.keyLimitReached.rawValue: return NSLocalizedString("Too many public WireGuard keys", comment: "") - case RestErrorCode.invalidAccount.rawValue: + case Code.invalidAccount.rawValue: return NSLocalizedString("Invalid account", comment: "") default: return nil @@ -46,12 +61,16 @@ struct ServerErrorResponse: LocalizedError, Decodable, RestResponse { var recoverySuggestion: String? { switch code { - case RestErrorCode.keyLimitReached.rawValue: + case Code.keyLimitReached.rawValue: return NSLocalizedString("Remove unused WireGuard keys", comment: "") default: return nil } } + + static func == (lhs: Self, rhs: Self) -> Bool { + return lhs.code == rhs.code + } } /// An error type returned by `MullvadRest` |
