SPB Git

Repository Public

Errors.swift 2.4 KB 60 lines swift

1import Foundation23/// Every failure of `SPMailClient`.4public enum SPMailError: Error, Sendable, CustomStringConvertible {5    /// The API answered with a non-2xx status.6    case api(APIError)7    /// No answer (network, TLS, timeout).8    case transport(URLError)9    /// A 2xx answer that does not match the expected model.10    case decoding(String)11    /// A webhook signature that does not verify.12    case invalidSignature(String)13    /// `waitForDelivery` gave up; the associated value is the last status seen.14    case deliveryTimeout(OutboundMessage)15    /// The client was misconfigured (empty key, plain http on a remote host…).16    case configuration(String)1718    public var description: String {19        switch self {20        case .api(let e): return "SP Mail \(e.status) \(e.code): \(e.message)" + (e.requestID.map { " (request \($0))" } ?? "")21        case .transport(let e): return "SP Mail unreachable: \(e.localizedDescription)"22        case .decoding(let m): return "SP Mail answer could not be decoded: \(m)"23        case .invalidSignature(let m): return "Webhook signature verification failed: \(m)"24        case .deliveryTimeout(let o): return "Send \(o.id) is still \(o.status.rawValue)"25        case .configuration(let m): return "SPMailClient configuration: \(m)"26        }27    }28}2930/// The error envelope of the API: `{"error": {"code", "message", "details"}}` plus the HTTP status.31public struct APIError: Error, Sendable, Equatable {32    public enum Kind: Sendable, Equatable {33        case badRequest, unauthorized, forbidden, notFound, conflict, rateLimited, server, other34    }35    /// HTTP status.36    public let status: Int37    /// Stable machine code (`from_not_allowed`, `insufficient_scope`, `not_found`…; docs/API.md §15).38    public let code: String39    public let message: String40    /// `X-Request-Id`: quote it when reporting a problem.41    public let requestID: String?42    /// Seconds announced by `Retry-After`.43    public let retryAfter: TimeInterval?44    /// `details` of the envelope, as raw JSON text.45    public let detailsJSON: String?4647    public var kind: Kind {48        switch status {49        case 400: return .badRequest50        case 401: return .unauthorized51        case 403: return .forbidden52        case 404: return .notFound53        case 409: return .conflict54        case 429: return .rateLimited55        case 500...599: return .server56        default: return .other57        }58    }59}60
Files main
Menu

Theme