use std::convert::Infallible;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum InstrumentError {
#[error("instrument does not exist")]
NotFound,
}
#[derive(Debug, Error)]
pub enum ExchangeError {
#[error("layer: {0}")]
Layer(#[from] Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "http")]
#[error("http: {0}")]
Http(hyper::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
#[error("api: {0}")]
Api(anyhow::Error),
#[error("unavailable: {0}")]
Unavailable(anyhow::Error),
#[error("instrument: {0}")]
Instrument(InstrumentError),
#[error("rate limited: {0}")]
RateLimited(anyhow::Error),
#[error("key error: {0}")]
KeyError(anyhow::Error),
#[error("order not found")]
OrderNotFound,
#[error("forbidden: {0}")]
Forbidden(anyhow::Error),
#[error("unexpected response type: {0}")]
UnexpectedResponseType(String),
}
impl ExchangeError {
pub fn is_temporary(&self) -> bool {
#[cfg(feature = "http")]
{
matches!(
self,
Self::RateLimited(_) | Self::Unavailable(_) | Self::Http(_)
)
}
#[cfg(not(feature = "http"))]
{
matches!(self, Self::RateLimited(_) | Self::Unavailable(_))
}
}
pub fn flatten(self) -> Self {
match self {
Self::Layer(err) => match err.downcast::<Self>() {
Ok(err) => (*err).flatten(),
Err(err) => Self::Other(anyhow::anyhow!("{err}")),
},
err => err,
}
}
pub fn layer(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
match err.downcast::<Self>() {
Ok(err) => (*err).flatten(),
Err(err) => Self::Other(anyhow::anyhow!("{err}")),
}
}
pub fn unexpected_response_type(msg: impl ToString) -> Self {
Self::UnexpectedResponseType(msg.to_string())
}
}
impl From<Infallible> for ExchangeError {
fn from(_: Infallible) -> Self {
panic!("infallible")
}
}