aboutsummaryrefslogtreecommitdiff
path: root/src/util/error.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-12-14 13:55:11 +0100
committerAlex Auvolat <alex@adnab.me>2022-01-04 12:45:46 +0100
commit5b1117e582db16cc5aa50840a685875cbd5501f4 (patch)
tree06fec47bf56cb08cb51334454dc15f98352c98f2 /src/util/error.rs
parent8f6026de5ecd44cbe0fc0bcd47638a1ece860439 (diff)
downloadgarage-5b1117e582db16cc5aa50840a685875cbd5501f4.tar.gz
garage-5b1117e582db16cc5aa50840a685875cbd5501f4.zip
New model for buckets
Diffstat (limited to 'src/util/error.rs')
-rw-r--r--src/util/error.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util/error.rs b/src/util/error.rs
index ff03d05b..08cf1302 100644
--- a/src/util/error.rs
+++ b/src/util/error.rs
@@ -119,6 +119,35 @@ where
}
}
+/// Trait to map error to the Bad Request error code
+pub trait OkOrMessage {
+ type S2;
+ fn ok_or_message<M: Into<String>>(self, message: M) -> Self::S2;
+}
+
+impl<T, E> OkOrMessage for Result<T, E>
+where
+ E: std::fmt::Display,
+{
+ type S2 = Result<T, Error>;
+ fn ok_or_message<M: Into<String>>(self, message: M) -> Result<T, Error> {
+ match self {
+ Ok(x) => Ok(x),
+ Err(e) => Err(Error::Message(format!("{}: {}", message.into(), e))),
+ }
+ }
+}
+
+impl<T> OkOrMessage for Option<T> {
+ type S2 = Result<T, Error>;
+ fn ok_or_message<M: Into<String>>(self, message: M) -> Result<T, Error> {
+ match self {
+ Some(x) => Ok(x),
+ None => Err(Error::Message(message.into())),
+ }
+ }
+}
+
// 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