Skip to main content

agixt_sdk/
error.rs

1//! Error types for the AGiXT SDK.
2
3use std::fmt;
4
5/// Error types for AGiXT SDK operations.
6#[derive(Debug)]
7pub enum Error {
8    /// Error from the HTTP client
9    RequestError(reqwest::Error),
10    /// Error parsing JSON
11    JsonError(serde_json::Error),
12    /// Error from the AGiXT API
13    ApiError {
14        status: u16,
15        message: String,
16    },
17    /// Error with authentication
18    AuthError(String),
19    /// Invalid input parameters
20    InvalidInput(String),
21    /// Resource not found
22    NotFound(String),
23    /// Generic error for other cases
24    Other(String),
25}
26
27impl std::error::Error for Error {}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Error::RequestError(e) => write!(f, "Request error: {}", e),
33            Error::JsonError(e) => write!(f, "JSON error: {}", e),
34            Error::ApiError { status, message } => {
35                write!(f, "API error ({}): {}", status, message)
36            }
37            Error::AuthError(msg) => write!(f, "Authentication error: {}", msg),
38            Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
39            Error::NotFound(msg) => write!(f, "Not found: {}", msg),
40            Error::Other(msg) => write!(f, "Error: {}", msg),
41        }
42    }
43}
44
45impl From<reqwest::Error> for Error {
46    fn from(err: reqwest::Error) -> Self {
47        Error::RequestError(err)
48    }
49}
50
51impl From<serde_json::Error> for Error {
52    fn from(err: serde_json::Error) -> Self {
53        Error::JsonError(err)
54    }
55}
56
57/// Result type alias using the AGiXT Error type.
58pub type Result<T> = std::result::Result<T, Error>;