Download this file
| 1 | import Foundation |
| 2 | |
| 3 | /// What can go wrong talking to the person API. |
| 4 | public enum EquipoAPIError: Error, Sendable, Equatable { |
| 5 | /// The request never reached the server or its answer never came back. |
| 6 | case offline |
| 7 | /// The server answered with an error body. |
| 8 | case server(status: Int, body: APIErrorBody) |
| 9 | /// The server answered with an error but no readable body. |
| 10 | case status(Int) |
| 11 | /// A card the person tried to answer changed or was already answered. |
| 12 | case card(status: Int, code: String, card: ThinCard) |
| 13 | case tooLarge |
| 14 | case undecodable |
| 15 | case badOrigin |
| 16 | case signedOut |
| 17 | |
| 18 | public var code: String? { |
| 19 | switch self { |
| 20 | case .server(_, let body): return body.code |
| 21 | case .card(_, let code, _): return code |
| 22 | default: return nil |
| 23 | } |
| 24 | } |
| 25 | public var status: Int? { |
| 26 | switch self { |
| 27 | case .server(let s, _), .status(let s), .card(let s, _, _): return s |
| 28 | default: return nil |
| 29 | } |
| 30 | } |
| 31 | /// The session is over: sign in again. |
| 32 | public var endsSession: Bool { status == 401 || self == .signedOut } |
| 33 | /// The resource changed: fetch it before deciding again. |
| 34 | public var isStale: Bool { ["revision_conflict", "card_stale", "card_answered"].contains(code ?? "") } |
| 35 | public var currentRevision: Int64? { if case .server(_, let body) = self { return body.currentRevision }; return nil } |
| 36 | } |
| 37 | |