carp_cli/utils/
error.rs

1use std::fmt;
2
3/// Result type alias for Carp CLI operations
4pub type CarpResult<T> = Result<T, CarpError>;
5
6/// Main error type for the Carp CLI
7#[derive(Debug)]
8pub enum CarpError {
9    /// IO-related errors
10    Io(std::io::Error),
11    /// HTTP request errors
12    Http(reqwest::Error),
13    /// JSON serialization/deserialization errors
14    Json(serde_json::Error),
15    /// TOML parsing errors
16    Toml(toml::de::Error),
17    /// Configuration errors
18    Config(String),
19    /// Authentication errors
20    Auth(String),
21    /// API errors with status code and message
22    Api { status: u16, message: String },
23    /// Agent not found
24    #[allow(dead_code)]
25    AgentNotFound(String),
26    /// Invalid agent name or version
27    InvalidAgent(String),
28    /// Manifest parsing errors
29    ManifestError(String),
30    /// File system errors
31    FileSystem(String),
32    /// Network connectivity errors
33    #[allow(dead_code)]
34    Network(String),
35    /// Generic errors with custom message
36    Other(String),
37}
38
39impl fmt::Display for CarpError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            CarpError::Io(e) => write!(f, "IO error: {e}"),
43            CarpError::Http(e) => write!(f, "HTTP error: {e}"),
44            CarpError::Json(e) => write!(f, "JSON error: {e}"),
45            CarpError::Toml(e) => write!(f, "TOML error: {e}"),
46            CarpError::Config(msg) => write!(f, "Configuration error: {msg}"),
47            CarpError::Auth(msg) => write!(f, "Authentication error: {msg}"),
48            CarpError::Api { status, message } => {
49                write!(f, "API error ({status}): {message}")
50            }
51            CarpError::AgentNotFound(name) => write!(f, "Agent '{name}' not found"),
52            CarpError::InvalidAgent(msg) => write!(f, "Invalid agent: {msg}"),
53            CarpError::ManifestError(msg) => write!(f, "Manifest error: {msg}"),
54            CarpError::FileSystem(msg) => write!(f, "File system error: {msg}"),
55            CarpError::Network(msg) => write!(f, "Network error: {msg}"),
56            CarpError::Other(msg) => write!(f, "{msg}"),
57        }
58    }
59}
60
61impl std::error::Error for CarpError {
62    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63        match self {
64            CarpError::Io(e) => Some(e),
65            CarpError::Http(e) => Some(e),
66            CarpError::Json(e) => Some(e),
67            CarpError::Toml(e) => Some(e),
68            _ => None,
69        }
70    }
71}
72
73impl From<std::io::Error> for CarpError {
74    fn from(err: std::io::Error) -> Self {
75        CarpError::Io(err)
76    }
77}
78
79impl From<reqwest::Error> for CarpError {
80    fn from(err: reqwest::Error) -> Self {
81        CarpError::Http(err)
82    }
83}
84
85impl From<serde_json::Error> for CarpError {
86    fn from(err: serde_json::Error) -> Self {
87        CarpError::Json(err)
88    }
89}
90
91impl From<toml::de::Error> for CarpError {
92    fn from(err: toml::de::Error) -> Self {
93        CarpError::Toml(err)
94    }
95}
96
97impl From<zip::result::ZipError> for CarpError {
98    fn from(err: zip::result::ZipError) -> Self {
99        CarpError::FileSystem(format!("ZIP error: {err}"))
100    }
101}