Skip to main content

frm/
errors.rs

1// Copyright (c) 2025-2026 Michael S. Klishin and Contributors
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::io;
10
11use bel7_cli::{ExitCode, ExitCodeProvider};
12use thiserror::Error;
13
14use crate::version::Version;
15
16#[derive(Error, Debug)]
17pub enum Error {
18    #[error("version {0} is not installed")]
19    VersionNotInstalled(Version),
20
21    #[error("version {0} is already installed")]
22    VersionAlreadyInstalled(Version),
23
24    #[error("invalid version format: {0}")]
25    InvalidVersion(String),
26
27    #[error("download failed: {0}")]
28    DownloadFailed(String),
29
30    #[error("release not found: {0}")]
31    ReleaseNotFound(String),
32
33    #[error("extraction failed: {0}")]
34    ExtractionFailed(String),
35
36    #[error("configuration error: {0}")]
37    Config(String),
38
39    #[error("unknown tool: {0}")]
40    UnknownTool(String),
41
42    #[error("unknown config file: {0}")]
43    UnknownConfigFile(String),
44
45    #[error("file not found: {0}")]
46    FileNotFound(String),
47
48    #[error("command failed: {0}")]
49    CommandFailed(String),
50
51    #[error("I/O error: {0}")]
52    Io(#[from] io::Error),
53
54    #[error("HTTP error: {0}")]
55    Http(#[from] reqwest::Error),
56
57    #[error("TOML parse error: {0}")]
58    TomlParse(#[from] toml::de::Error),
59
60    #[error("TOML serialization error: {0}")]
61    TomlSerialize(#[from] toml::ser::Error),
62
63    #[error("JSON error: {0}")]
64    Json(#[from] serde_json::Error),
65
66    #[error("expected an alpha version, got: {0}")]
67    ExpectedAlphaVersion(Version),
68
69    #[error("expected a non-alpha version, got: {0}")]
70    ExpectedNonAlphaVersion(Version),
71
72    #[error("no alpha releases found")]
73    NoAlphaReleasesFound,
74
75    #[error("invalid date/time: {0}")]
76    InvalidDateTime(String),
77
78    #[error("version mismatch: expected {expected}, detected {detected} in tarball filename")]
79    TanzuVersionMismatch {
80        expected: Version,
81        detected: Version,
82    },
83}
84
85impl ExitCodeProvider for Error {
86    fn exit_code(&self) -> ExitCode {
87        match self {
88            Error::VersionNotInstalled(_) => ExitCode::NoInput,
89            Error::VersionAlreadyInstalled(_) => ExitCode::CantCreat,
90            Error::InvalidVersion(_) => ExitCode::Usage,
91            Error::DownloadFailed(_) => ExitCode::Unavailable,
92            Error::ReleaseNotFound(_) => ExitCode::NoInput,
93            Error::ExtractionFailed(_) => ExitCode::Software,
94            Error::Config(_) => ExitCode::Config,
95            Error::UnknownTool(_) => ExitCode::Usage,
96            Error::UnknownConfigFile(_) => ExitCode::Usage,
97            Error::FileNotFound(_) => ExitCode::NoInput,
98            Error::CommandFailed(_) => ExitCode::Software,
99            Error::Io(_) => ExitCode::IoErr,
100            Error::Http(_) => ExitCode::Protocol,
101            Error::TomlParse(_) => ExitCode::DataErr,
102            Error::TomlSerialize(_) => ExitCode::Software,
103            Error::Json(_) => ExitCode::DataErr,
104            Error::ExpectedAlphaVersion(_) => ExitCode::Usage,
105            Error::ExpectedNonAlphaVersion(_) => ExitCode::Usage,
106            Error::NoAlphaReleasesFound => ExitCode::NoInput,
107            Error::InvalidDateTime(_) => ExitCode::Usage,
108            Error::TanzuVersionMismatch { .. } => ExitCode::DataErr,
109        }
110    }
111}