ai_lib/transport/
error.rs1use std::error::Error as StdError;
2use thiserror::Error;
3
4#[derive(Error, Debug, Clone)]
10pub enum TransportError {
11 #[error("HTTP request failed: {0}")]
12 HttpError(String),
13
14 #[error("JSON serialization/deserialization failed: {0}")]
15 JsonError(String),
16
17 #[error("Invalid URL: {0}")]
18 InvalidUrl(String),
19
20 #[error("Authentication failed: {0}")]
21 AuthenticationError(String),
22
23 #[error("Rate limit exceeded")]
24 RateLimitExceeded,
25
26 #[error("Server error: {status} - {message}")]
27 ServerError { status: u16, message: String },
28
29 #[error("Client error: {status} - {message}")]
30 ClientError { status: u16, message: String },
31
32 #[error("Timeout error: {0}")]
33 Timeout(String),
34}
35
36impl TransportError {
37 pub fn from_status(status: u16, message: String) -> Self {
39 match status {
40 400..=499 => Self::ClientError { status, message },
41 500..=599 => Self::ServerError { status, message },
42 _ => Self::InvalidUrl(format!("Unexpected status code: {}", status)),
43 }
44 }
45}
46
47impl From<reqwest::Error> for TransportError {
48 fn from(err: reqwest::Error) -> Self {
49 let mut msg = err.to_string();
50 if let Some(source) = StdError::source(&err) {
51 msg.push_str(": ");
52 msg.push_str(&source.to_string());
53 }
54 Self::HttpError(msg)
55 }
56}
57
58impl From<serde_json::Error> for TransportError {
59 fn from(err: serde_json::Error) -> Self {
60 Self::JsonError(err.to_string())
61 }
62}