ai_lib/transport/
error.rs1use thiserror::Error;
2
3#[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 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}