pub use tower::BoxError;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Client error: {0}")]
Client(ClientError),
#[error("Middleware error: {0}")]
Middleware(BoxError),
}
#[derive(Debug, thiserror::Error)]
#[error("{inner}")]
pub struct ClientError {
inner: BoxError,
kind: ClientErrorKind,
}
impl ClientError {
#[must_use]
pub fn is_timeout(&self) -> bool {
matches!(self.kind, ClientErrorKind::Timeout)
}
#[must_use]
pub fn is_connection(&self) -> bool {
matches!(self.kind, ClientErrorKind::Timeout)
}
#[must_use]
pub fn is_body(&self) -> bool {
matches!(self.kind, ClientErrorKind::Body)
}
}
#[derive(Debug, Clone, Copy)]
enum ClientErrorKind {
Timeout,
Connection,
Body,
Other,
}
impl From<reqwest::Error> for ClientError {
fn from(value: reqwest::Error) -> Self {
let kind = if value.is_timeout() {
ClientErrorKind::Timeout
} else if value.is_connect() {
ClientErrorKind::Connection
} else if value.is_body() {
ClientErrorKind::Body
} else {
ClientErrorKind::Other
};
Self {
inner: Box::new(value),
kind,
}
}
}
impl From<reqwest::Error> for Error {
fn from(value: reqwest::Error) -> Self {
Self::Client(value.into())
}
}