SPB Git

Repository Public

DriveError.swift 4.5 KB 75 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/API/DriveError.swift3// Description: The errors SP Drive distinguishes: unreachable server, ended session, an answer of the server with its stable code, damaged content, local failures.4import Foundation56public enum DriveError: Error, Sendable, Equatable, LocalizedError {7    /// No network, or the server did not answer. Nothing was decided: the operation can be tried again.8    case offline(String)9    /// The session is over (revoked, expired) — the person must sign in again.10    case unauthenticated11    /// The server refused; `code` is stable ("name_conflict", "version_conflict", "quota_exceeded", "not_found"…).12    case server(status: Int, code: String, message: String, extra: [String: JSONValue])13    case invalidResponse(String)14    /// Bytes received or stored do not match their SHA-256 or their size.15    case integrity(String)16    case cancelled17    case local(String)1819    public var code: String {20        switch self {21        case .offline: return "offline"22        case .unauthenticated: return "unauthenticated"23        case .server(_, let code, _, _): return code24        case .invalidResponse: return "invalid_response"25        case .integrity: return "integrity"26        case .cancelled: return "cancelled"27        case .local: return "local"28        }29    }30    public var status: Int? { if case .server(let s, _, _, _) = self { return s }; return nil }31    public var extra: [String: JSONValue] { if case .server(_, _, _, let e) = self { return e }; return [:] }32    /// True when trying again later can succeed without anyone deciding anything (network, overload, server restart).33    public var isTransient: Bool {34        switch self {35        case .offline: return true36        case .server(let status, let code, _, _): return status >= 500 || status == 429 || code == "rate_limited"37        default: return false38        }39    }40    public var isNotFound: Bool { code == "not_found" }41    public var errorDescription: String? {42        switch self {43        case .offline: return "Serveur injoignable. L’opération reprendra dès le retour de la connexion."44        case .unauthenticated: return "La session est terminée. Reconnectez-vous."45        case .server(_, let code, let message, _): return DriveError.french[code] ?? message46        case .invalidResponse(let m): return "Réponse inattendue du serveur (\(m))."47        case .integrity(let m): return "Contrôle d’intégrité échoué : \(m)."48        case .cancelled: return "Opération annulée."49        case .local(let m): return m50        }51    }52    private static let french: [String: String] = [53        "name_conflict": "Un élément porte déjà ce nom à cet endroit.", "version_conflict": "Le fichier a changé sur le serveur entre-temps.",54        "quota_exceeded": "Espace de stockage insuffisant sur SP Drive.", "file_too_large": "Ce fichier dépasse la taille maximale acceptée.",55        "not_found": "Élément introuvable (supprimé ou déplacé).", "infected": "Ce fichier a été bloqué par l’antivirus.",56        "move_into_self": "Un dossier ne peut pas être déplacé dans lui-même.", "root_immutable": "Le dossier racine ne peut pas être modifié.",57        "rate_limited": "Trop de requêtes : nouvel essai dans un instant.", "reauth_required": "Confirmez avec votre passkey.",58        "authentication_failed": "Passkey refusée.", "challenge_expired": "Demande expirée, recommencez.", "bad_expiry": "La date d’expiration doit être dans le futur.",59        "current_version": "La version courante ne peut pas être supprimée.", "upload_closed": "Ce téléversement n’est plus actif.",60        "checksum_mismatch": "Le fichier reçu par le serveur diffère du fichier envoyé.", "device_code_invalid": "Code inconnu ou expiré.",61    ]62    /// Maps anything thrown by Foundation networking into a DriveError.63    public static func wrap(_ error: Error) -> DriveError {64        if let d = error as? DriveError { return d }65        if error is CancellationError { return .cancelled }66        let ns = error as NSError67        if ns.domain == NSURLErrorDomain {68            if ns.code == NSURLErrorCancelled { return .cancelled }69            return .offline(ns.localizedDescription)70        }71        if ns.domain == NSPOSIXErrorDomain, [Int(ENETDOWN), Int(ENETUNREACH), Int(ECONNREFUSED), Int(ECONNRESET), Int(ETIMEDOUT), Int(EPIPE)].contains(ns.code) { return .offline(ns.localizedDescription) }72        return .local(ns.localizedDescription)73    }74}75
Files main
Menu

Theme