Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/API/APIError.swift3// Description: Errors of the SP Wallet API as the clients see them: the server's code and message, or a transport failure.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89public struct APIError: Error, Sendable, Equatable, LocalizedError {10 /** HTTP status; 0 when no answer was received (offline, timeout, TLS failure). */11 public let status: Int12 /** The server's machine-readable code (`unauthenticated`, `policy`, `bad_address`, `quote_moved`, …) or a client code (`offline`, `decoding`, `refused_redirect`). */13 public let code: String14 /** The server's English sentence. Screens translate `code` when they know it and fall back to this. */15 public let message: String16 /** Machine-readable detail of a refusal when the server sends one (e.g. the fresh quote of `quote_moved`). */17 public let data: JSONValue?18 /** The whole JSON body of the error answer (risk refusals carry their reason there). */19 public let body: JSONValue?2021 public init(status: Int, code: String, message: String, data: JSONValue? = nil, body: JSONValue? = nil) {22 self.status = status; self.code = code; self.message = message; self.data = data; self.body = body23 }2425 public static func offline(_ detail: String = "offline") -> APIError { APIError(status: 0, code: "offline", message: detail) }26 public var isOffline: Bool { status == 0 && code == "offline" }27 public var isUnauthenticated: Bool { status == 401 && code == "unauthenticated" }28 public var errorDescription: String? { message }29}30