diff options
author | Alex Auvolat <alex@adnab.me> | 2020-12-02 13:30:47 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-12-02 13:30:47 +0100 |
commit | d4de2ffc40fe9d003f12139053ca070eda0b7221 (patch) | |
tree | e95476f0b7a6d1c75cc462b3ea7eee74c4faf09f /src/error.rs | |
download | netapp-d4de2ffc40fe9d003f12139053ca070eda0b7221.tar.gz netapp-d4de2ffc40fe9d003f12139053ca070eda0b7221.zip |
First commit
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..b54423a --- /dev/null +++ b/src/error.rs @@ -0,0 +1,57 @@ +use err_derive::Error; +use std::io; + +use log::error; + +#[derive(Debug, Error)] +pub enum Error { + #[error(display = "IO error: {}", _0)] + Io(#[error(source)] io::Error), + + #[error(display = "Messagepack encode error: {}", _0)] + RMPEncode(#[error(source)] rmp_serde::encode::Error), + #[error(display = "Messagepack decode error: {}", _0)] + RMPDecode(#[error(source)] rmp_serde::decode::Error), + + #[error(display = "Tokio join error: {}", _0)] + TokioJoin(#[error(source)] tokio::task::JoinError), + + #[error(display = "oneshot receive error: {}", _0)] + OneshotRecv(#[error(source)] tokio::sync::oneshot::error::RecvError), + + #[error(display = "Handshake error: {}", _0)] + Handshake(#[error(source)] kuska_handshake::async_std::Error), + + #[error(display = "{}", _0)] + Message(String), + + #[error(display = "Remote error: {}", _0)] + Remote(String), +} + +impl<T> From<tokio::sync::watch::error::SendError<T>> for Error { + fn from(_e: tokio::sync::watch::error::SendError<T>) -> Error { + Error::Message(format!("Watch send error")) + } +} + +impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error { + fn from(_e: tokio::sync::mpsc::error::SendError<T>) -> Error { + Error::Message(format!("MPSC send error")) + } +} + +pub trait LogError { + fn log_err(self, msg: &'static str); +} + +impl<E> LogError for Result<(), E> +where + E: Into<Error>, +{ + fn log_err(self, msg: &'static str) { + if let Err(e) = self { + error!("Error: {}: {}", msg, Into::<Error>::into(e)); + }; + } +} |