ai_lib/transport/
error.rs

1use thiserror::Error;
2
3/// Transport layer error types, unified encapsulation of HTTP and JSON errors
4///
5/// Transport layer error types with unified encapsulation of HTTP and JSON errors
6///
7/// Unified encapsulation of all HTTP-level errors and JSON parsing errors
8#[derive(Error, Debug, Clone)]
9pub enum TransportError {
10    #[error("HTTP request failed: {0}")]
11    HttpError(String),
12
13    #[error("JSON serialization/deserialization failed: {0}")]
14    JsonError(String),
15
16    #[error("Invalid URL: {0}")]
17    InvalidUrl(String),
18
19    #[error("Authentication failed: {0}")]
20    AuthenticationError(String),
21
22    #[error("Rate limit exceeded")]
23    RateLimitExceeded,
24
25    #[error("Server error: {status} - {message}")]
26    ServerError { status: u16, message: String },
27
28    #[error("Client error: {status} - {message}")]
29    ClientError { status: u16, message: String },
30
31    #[error("Timeout error: {0}")]
32    Timeout(String),
33}
34
35impl TransportError {
36    /// Create error from HTTP status code
37    pub fn from_status(status: u16, message: String) -> Self {
38        match status {
39            400..=499 => Self::ClientError { status, message },
40            500..=599 => Self::ServerError { status, message },
41            _ => Self::InvalidUrl(format!("Unexpected status code: {}", status)),
42        }
43    }
44}
45
46impl From<reqwest::Error> for TransportError {
47    fn from(err: reqwest::Error) -> Self {
48        Self::HttpError(err.to_string())
49    }
50}
51
52impl From<serde_json::Error> for TransportError {
53    fn from(err: serde_json::Error) -> Self {
54        Self::JsonError(err.to_string())
55    }
56}