SPB Git

Repository Public MIT

CLIError.swift 3.8 KB 95 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/GlassCLICore/CLIError.swift8// Module           : Glass CLICore9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import SPGlassRegistry4344/// Process exit codes of `spglass`.45public enum ExitCode: Int32, Sendable {46    case success = 047    case failure = 148    case usage = 249    /// Local modifications or dependents prevent the operation; nothing was changed.50    case conflict = 351    /// A checksum, size or signature did not match, or an installed file is missing.52    case integrity = 453    case network = 554    case notFound = 655    /// A manifest, index, configuration or lock file is invalid.56    case invalidData = 757    case interrupted = 13058}5960/// A failure reported to the user: a stable code, a sentence, an optional hint and optional details (a diff).61public struct CLIError: Error, Equatable, Sendable {62    public let code: String63    public let message: String64    public let hint: String?65    public let details: String?66    public let exitCode: ExitCode6768    public init(code: String, message: String, hint: String? = nil, details: String? = nil, exitCode: ExitCode = .failure) {69        self.code = code; self.message = message; self.hint = hint; self.details = details; self.exitCode = exitCode70    }7172    public init(_ error: RegistryError) {73        let exit: ExitCode74        switch error {75        case .hashMismatch, .sizeMismatch, .signatureMissing, .signatureInvalid: exit = .integrity76        case .unknownItem, .unknownVersion: exit = .notFound77        case .invalidIdentifier, .invalidVersion: exit = .usage78        default: exit = .invalidData79        }80        self.init(code: error.code, message: error.description, exitCode: exit)81    }8283    public static func usage(_ message: String, hint: String? = "Run `spglass --help`.") -> CLIError {84        CLIError(code: "usage", message: message, hint: hint, exitCode: .usage)85    }8687    /// Converts any error thrown inside a command.88    public static func wrap(_ error: any Error) -> CLIError {89        if let error = error as? CLIError { return error }90        if let error = error as? RegistryError { return CLIError(error) }91        if error is CancellationError { return CLIError(code: "interrupted", message: "Interrupted. No change was kept.", exitCode: .interrupted) }92        return CLIError(code: "unexpected", message: "Unexpected error: \(error.localizedDescription)")93    }94}95
Files main
Menu

Theme