use thiserror::Error;
#[derive(Debug, Error)]
pub enum RequestError {
#[error("Connection error: {0}")]
Connection(#[source] reqwest::Error),
#[error("Request timed out: {0}")]
Timeout(#[source] reqwest::Error),
#[error("Network transport error: {0}")]
Transport(#[source] reqwest::Error),
#[error("Failed to build request: {0}")]
Build(#[source] reqwest::Error),
#[error("Event stream error: {0}")]
EventSource(String),
}
impl From<reqwest::Error> for RequestError {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() {
Self::Timeout(err)
} else if err.is_connect() {
Self::Connection(err)
} else if err.is_builder() {
Self::Build(err)
} else {
Self::Transport(err)
}
}
}
impl RequestError {
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_))
}
pub fn is_connection(&self) -> bool {
matches!(self, Self::Connection(_))
}
pub fn status(&self) -> Option<reqwest::StatusCode> {
match self {
Self::Connection(e) | Self::Timeout(e) | Self::Transport(e) | Self::Build(e) => {
e.status()
}
Self::EventSource(_) => None,
}
}
pub fn is_retryable(&self) -> bool {
self.is_timeout() || self.is_connection()
}
}