use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("Configuration error: {0}")]
Config(String),
#[error("I/O error: {source}")]
Io {
#[from]
source: std::io::Error,
},
#[error("Network error: {0}")]
Network(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Authentication failed: {0}")]
Auth(String),
#[error("Operation timed out")]
Timeout,
#[error("Server error: {0}")]
Server(String),
#[error("Unexpected error: {0}")]
Other(String),
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() {
Error::Timeout
} else if err.is_connect() {
Error::Network(format!("Connection error: {err}"))
} else if err.is_decode() {
Error::Serialization(format!("Failed to decode response: {err}"))
} else {
Error::Network(err.to_string())
}
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Serialization(err.to_string())
}
}
impl From<url::ParseError> for Error {
fn from(err: url::ParseError) -> Self {
Error::Config(format!("Invalid URL: {err}"))
}
}