use crate::StatusCode;
use std::{error, result};
use thiserror::Error;
macro_rules! impl_error {
($ty:ty, $($b:tt)*) => {
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to create client")]
Client(#[source] $ty),
#[error("Invalid request")]
Request(#[source] $ty),
#[error("Failed to execute request")]
Response(#[source] $ty),
#[error("Failed to read response body")]
Body(#[source] $ty),
#[error("HTTP {0}")]
Status(StatusCode),
}
impl Error {
pub fn client<E>(err: E) -> Self
where
E: $($b)*,
{
Error::Client(Box::new(err))
}
pub fn request<E>(err: E) -> Self
where
E: $($b)*,
{
Error::Request(Box::new(err))
}
pub fn response<E>(err: E) -> Self
where
E: $($b)*,
{
Error::Response(Box::new(err))
}
pub fn body<E>(err: E) -> Self
where
E: $($b)*,
{
Error::Body(Box::new(err))
}
}
};
}
#[cfg(not(feature = "local-error"))]
impl_error!(
Box<dyn error::Error + 'static + Send + Sync>,
error::Error + 'static + Send + Sync
);
#[cfg(feature = "local-error")]
impl_error!(Box<dyn error::Error + 'static>, error::Error + 'static);
pub type Result<T> = result::Result<T, Error>;