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    AgentNotFound(String),
25    /// Invalid agent name or version
26    InvalidAgent(String),
27    /// Manifest parsing errors
28    ManifestError(String),
29    /// File system errors
30    FileSystem(String),
31    /// Network connectivity errors
32    Network(String),
33    /// Generic errors with custom message
34    Other(String),
35}
36
37impl fmt::Display for CarpError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            CarpError::Io(e) => write!(f, "IO error: {}", e),
41            CarpError::Http(e) => write!(f, "HTTP error: {}", e),
42            CarpError::Json(e) => write!(f, "JSON error: {}", e),
43            CarpError::Toml(e) => write!(f, "TOML error: {}", e),
44            CarpError::Config(msg) => write!(f, "Configuration error: {}", msg),
45            CarpError::Auth(msg) => write!(f, "Authentication error: {}", msg),
46            CarpError::Api { status, message } => {
47                write!(f, "API error ({}): {}", status, message)
48            }
49            CarpError::AgentNotFound(name) => write!(f, "Agent '{}' not found", name),
50            CarpError::InvalidAgent(msg) => write!(f, "Invalid agent: {}", msg),
51            CarpError::ManifestError(msg) => write!(f, "Manifest error: {}", msg),
52            CarpError::FileSystem(msg) => write!(f, "File system error: {}", msg),
53            CarpError::Network(msg) => write!(f, "Network error: {}", msg),
54            CarpError::Other(msg) => write!(f, "{}", msg),
55        }
56    }
57}
58
59impl std::error::Error for CarpError {
60    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61        match self {
62            CarpError::Io(e) => Some(e),
63            CarpError::Http(e) => Some(e),
64            CarpError::Json(e) => Some(e),
65            CarpError::Toml(e) => Some(e),
66            _ => None,
67        }
68    }
69}
70
71impl From<std::io::Error> for CarpError {
72    fn from(err: std::io::Error) -> Self {
73        CarpError::Io(err)
74    }
75}
76
77impl From<reqwest::Error> for CarpError {
78    fn from(err: reqwest::Error) -> Self {
79        CarpError::Http(err)
80    }
81}
82
83impl From<serde_json::Error> for CarpError {
84    fn from(err: serde_json::Error) -> Self {
85        CarpError::Json(err)
86    }
87}
88
89impl From<toml::de::Error> for CarpError {
90    fn from(err: toml::de::Error) -> Self {
91        CarpError::Toml(err)
92    }
93}
94
95impl From<zip::result::ZipError> for CarpError {
96    fn from(err: zip::result::ZipError) -> Self {
97        CarpError::FileSystem(format!("ZIP error: {}", err))
98    }
99}