use std::error::Error as StdError;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum TransportError {
#[error("HTTP request failed: {0}")]
HttpError(String),
#[error("JSON serialization/deserialization failed: {0}")]
JsonError(String),
#[error("Invalid URL: {0}")]
InvalidUrl(String),
#[error("Authentication failed: {0}")]
AuthenticationError(String),
#[error("Rate limit exceeded")]
RateLimitExceeded,
#[error("Server error: {status} - {message}")]
ServerError { status: u16, message: String },
#[error("Client error: {status} - {message}")]
ClientError { status: u16, message: String },
#[error("Timeout error: {0}")]
Timeout(String),
}
impl TransportError {
pub fn from_status(status: u16, message: String) -> Self {
match status {
400..=499 => Self::ClientError { status, message },
500..=599 => Self::ServerError { status, message },
_ => Self::InvalidUrl(format!("Unexpected status code: {}", status)),
}
}
}
impl From<reqwest::Error> for TransportError {
fn from(err: reqwest::Error) -> Self {
let mut msg = err.to_string();
if let Some(source) = StdError::source(&err) {
msg.push_str(": ");
msg.push_str(&source.to_string());
}
Self::HttpError(msg)
}
}
impl From<serde_json::Error> for TransportError {
fn from(err: serde_json::Error) -> Self {
Self::JsonError(err.to_string())
}
}