#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("Invalid URL: {0}")]
InvalidUrl(String),
#[error("Network error: {0}")]
NetworkError(#[from] reqwest::Error),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("HTTP {status}: {body}")]
HttpError {
status: u16,
body: String,
},
#[error("Empty response from server")]
EmptyResponse,
#[error("Request timed out")]
Timeout,
}
impl ApiError {
pub fn is_network_error(&self) -> bool {
matches!(self, ApiError::NetworkError(_))
}
pub fn is_timeout(&self) -> bool {
matches!(self, ApiError::Timeout)
}
pub fn status_code(&self) -> Option<u16> {
match self {
ApiError::HttpError { status, .. } => Some(*status),
_ => None,
}
}
}