use thiserror::Error;
use crate::BoxError;
use chateau::client::conn::ConnectionError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("connection: {0}")]
Connection(#[source] BoxError),
#[error("transport: {0}")]
Transport(#[source] BoxError),
#[error("protocol: {0}")]
Protocol(#[source] BoxError),
#[error("serivce: {0}")]
Service(#[source] BoxError),
#[error("user error: {0}")]
User(#[source] hyper::Error),
#[error("invalid method: {0}")]
InvalidMethod(http::Method),
#[error("unsupported protocol")]
UnsupportedProtocol,
#[error("request timeout")]
RequestTimeout,
}
impl<T, P, S> From<ConnectionError<T, P, S>> for Error
where
T: Into<BoxError>,
P: Into<BoxError>,
S: Into<BoxError>,
{
fn from(error: ConnectionError<T, P, S>) -> Self {
match error {
ConnectionError::Connecting(error) => Error::Transport(error.into()),
ConnectionError::Handshaking(error) => Error::Protocol(error.into()),
ConnectionError::Service(error) => Error::Connection(error.into()),
ConnectionError::Unavailable => {
Error::Connection("pool closed, no connection can be made".into())
}
ConnectionError::Key(error) => Error::Connection(error.into()),
_ => Error::Connection("Unknown error occured".into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::assert_impl_all;
assert_impl_all!(Error: std::error::Error, Send, Sync, Into<BoxError>);
}