diff options
author | Alex Auvolat <alex@adnab.me> | 2021-10-19 16:16:10 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-10-25 14:21:48 +0200 |
commit | de4276202ad2be8a2e07f2a6f2f48d9c25cdc32c (patch) | |
tree | 2e156181891c312c6b64122250ff9b5c25c91bef /src/util/error.rs | |
parent | 1b450c4b493dfcb2ee88acbca3ea584beac8eb4b (diff) | |
download | garage-de4276202ad2be8a2e07f2a6f2f48d9c25cdc32c.tar.gz garage-de4276202ad2be8a2e07f2a6f2f48d9c25cdc32c.zip |
Improve CLI, adapt tests, update documentation
Diffstat (limited to 'src/util/error.rs')
-rw-r--r-- | src/util/error.rs | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/util/error.rs b/src/util/error.rs index 390327f1..626958da 100644 --- a/src/util/error.rs +++ b/src/util/error.rs @@ -47,8 +47,14 @@ pub enum Error { #[error(display = "Timeout")] Timeout, - #[error(display = "Too many errors: {:?}", _0)] - TooManyErrors(Vec<String>), + #[error( + display = "Could not reach quorum of {}. {} of {} request succeeded, others returned errors: {:?}", + _0, + _1, + _2, + _3 + )] + Quorum(usize, usize, usize, Vec<String>), #[error(display = "Bad RPC: {}", _0)] BadRpc(String), @@ -81,6 +87,35 @@ impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error { } } +impl<'a> From<&'a str> for Error { + fn from(v: &'a str) -> Error { + Error::Message(v.to_string()) + } +} + +impl From<String> for Error { + fn from(v: String) -> Error { + Error::Message(v) + } +} + +pub trait ErrorContext<T, E> { + fn err_context<C: std::borrow::Borrow<str>>(self, ctx: C) -> Result<T, Error>; +} + +impl<T, E> ErrorContext<T, E> for Result<T, E> +where + E: std::fmt::Display, +{ + #[inline] + fn err_context<C: std::borrow::Borrow<str>>(self, ctx: C) -> Result<T, Error> { + match self { + Ok(x) => Ok(x), + Err(e) => Err(Error::Message(format!("{}\n{}", ctx.borrow(), e))), + } + } +} + // Custom serialization for our error type, for use in RPC. // Errors are serialized as a string of their Display representation. // Upon deserialization, they all become a RemoteError with the |