use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Authentication error: {0}")]
Authentication(String),
#[error("Rate limit exceeded: {0}")]
RateLimit(String),
#[error("Insufficient balance: {0}")]
InsufficientBalance(String),
#[error("Bad request: {0}")]
BadRequest(String),
#[error("Internal server error: {0}")]
InternalServer(String),
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Request timed out")]
Timeout,
#[error("Connection failed: {0}")]
Connection(String),
#[error("Unknown error: {0}")]
Unknown(String),
}
impl Error {
pub fn is_authentication(&self) -> bool {
matches!(self, Error::Authentication(_))
}
pub fn is_rate_limit(&self) -> bool {
matches!(self, Error::RateLimit(_))
}
pub fn is_insufficient_balance(&self) -> bool {
matches!(self, Error::InsufficientBalance(_))
}
pub fn is_bad_request(&self) -> bool {
matches!(self, Error::BadRequest(_))
}
pub fn is_server_error(&self) -> bool {
matches!(self, Error::InternalServer(_))
}
pub fn is_retryable(&self) -> bool {
matches!(self, Error::RateLimit(_) | Error::InternalServer(_) | Error::Timeout)
}
}