ai_lib/transport/
error.rs

1use thiserror::Error;
2
3/// 传输层错误类型,统一封装HTTP和JSON错误
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)]
9pub enum TransportError {
10    #[error("HTTP request failed: {0}")]
11    HttpError(#[from] reqwest::Error),
12    
13    #[error("JSON serialization/deserialization failed: {0}")]
14    JsonError(#[from] serde_json::Error),
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}