use crate::error::{ErrCode, Error};
use std::fmt;
macro_rules! dep_error {
($error:ty, $kind:expr, $code:expr, $reason:expr) => {
impl From<$error> for Error {
#[must_use]
fn from(inner: $error) -> Self {
Self::new($code, $reason, Some($kind(inner)))
}
}
};
}
dep_error!(
std::env::VarError,
ErrSource::Var,
ErrCode::Env,
"There was an error processing your enviroment"
);
dep_error!(
std::io::Error,
ErrSource::Io,
ErrCode::Io,
"There was an error processing your request"
);
dep_error!(
tokio::sync::mpsc::error::SendError<bytes::Bytes>,
ErrSource::SendBytes,
ErrCode::Io,
"There was an error sending out bytes"
);
#[cfg(test)]
dep_error!(
tokio::sync::mpsc::error::SendError<crate::client::Message>,
ErrSource::SendMessage,
ErrCode::Io,
"There was an error sending out bytes"
);
dep_error!(
std::num::TryFromIntError,
ErrSource::TryFromInt,
ErrCode::Parse,
"There was an error converting from an int"
);
#[cfg(test)]
dep_error!(
std::fmt::Error,
ErrSource::Fmt,
ErrCode::Io,
"There was an error in the formatting test"
);
#[cfg(test)]
dep_error!(
tokio::task::JoinError,
ErrSource::Join,
ErrCode::Protocol,
"There was an error at the tokio join point"
);
#[derive(Debug)]
#[allow(clippy::large_enum_variant, variant_size_differences)]
pub enum ErrSource {
#[cfg(test)]
Fmt(std::fmt::Error),
Io(std::io::Error),
#[cfg(test)]
Join(tokio::task::JoinError),
SendBytes(tokio::sync::mpsc::error::SendError<bytes::Bytes>),
#[cfg(test)]
SendMessage(tokio::sync::mpsc::error::SendError<crate::client::Message>),
TryFromInt(std::num::TryFromIntError),
Var(std::env::VarError),
}
impl std::error::Error for ErrSource {}
impl fmt::Display for ErrSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(test)]
Self::Fmt(source) => write!(f, "{}", source),
Self::Io(source) => write!(f, "{}", source),
#[cfg(test)]
Self::Join(source) => write!(f, "{}", source),
Self::SendBytes(source) => write!(f, "{}", source),
#[cfg(test)]
Self::SendMessage(source) => write!(f, "{}", source),
Self::TryFromInt(source) => write!(f, "{}", source),
Self::Var(source) => write!(f, "{}", source),
}
}
}