use std::time::Duration;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, HttpClientError>;
#[derive(Debug, Error)]
pub enum HttpClientError {
#[error("Request failed after {attempts} attempts: {message}")]
RetryExhausted {
attempts: u32,
message: String,
},
#[error("Circuit breaker is open, request rejected")]
CircuitOpen,
#[error("Request timed out after {0:?}")]
Timeout(Duration),
#[error("Connection error: {0}")]
Connection(String),
#[error("Invalid URL: {0}")]
InvalidUrl(String),
#[error("Failed to build request: {0}")]
RequestBuild(String),
#[error("Response error: {status} - {message}")]
Response {
status: u16,
message: String,
},
#[error("JSON error: {0}")]
Json(String),
#[error("Interceptor error: {0}")]
Interceptor(String),
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("URL parse error: {0}")]
UrlParse(#[from] url::ParseError),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}
impl HttpClientError {
pub fn is_retryable(&self) -> bool {
match self {
Self::Timeout(_) => true,
Self::Connection(_) => true,
Self::Http(e) => e.is_timeout() || e.is_connect(),
Self::Response { status, .. } => {
*status >= 500 || *status == 429
}
_ => false,
}
}
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_)) || matches!(self, Self::Http(e) if e.is_timeout())
}
pub fn is_connection(&self) -> bool {
matches!(self, Self::Connection(_)) || matches!(self, Self::Http(e) if e.is_connect())
}
pub fn status_code(&self) -> Option<u16> {
match self {
Self::Response { status, .. } => Some(*status),
Self::Http(e) => e.status().map(|s| s.as_u16()),
_ => None,
}
}
}