From d8ab5bdc3e20759e5ba8a6844393757da3539372 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 22 Dec 2021 18:50:08 +0100 Subject: New buckets for 0.6.0: fix model and migration --- src/api/error.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/api/error.rs') diff --git a/src/api/error.rs b/src/api/error.rs index 9bb8f8e2..828a2342 100644 --- a/src/api/error.rs +++ b/src/api/error.rs @@ -156,62 +156,67 @@ impl Error { /// Trait to map error to the Bad Request error code pub trait OkOrBadRequest { - type S2; - fn ok_or_bad_request(self, reason: &'static str) -> Self::S2; + type S; + fn ok_or_bad_request>(self, reason: M) -> Result; } impl OkOrBadRequest for Result where E: std::fmt::Display, { - type S2 = Result; - fn ok_or_bad_request(self, reason: &'static str) -> Result { + type S = T; + fn ok_or_bad_request>(self, reason: M) -> Result { match self { Ok(x) => Ok(x), - Err(e) => Err(Error::BadRequest(format!("{}: {}", reason, e))), + Err(e) => Err(Error::BadRequest(format!( + "{}: {}", + reason.as_ref(), + e.to_string() + ))), } } } impl OkOrBadRequest for Option { - type S2 = Result; - fn ok_or_bad_request(self, reason: &'static str) -> Result { + type S = T; + fn ok_or_bad_request>(self, reason: M) -> Result { match self { Some(x) => Ok(x), - None => Err(Error::BadRequest(reason.to_string())), + None => Err(Error::BadRequest(reason.as_ref().to_string())), } } } /// Trait to map an error to an Internal Error code pub trait OkOrInternalError { - type S2; - fn ok_or_internal_error(self, reason: &'static str) -> Self::S2; + type S; + fn ok_or_internal_error>(self, reason: M) -> Result; } impl OkOrInternalError for Result where E: std::fmt::Display, { - type S2 = Result; - fn ok_or_internal_error(self, reason: &'static str) -> Result { + type S = T; + fn ok_or_internal_error>(self, reason: M) -> Result { match self { Ok(x) => Ok(x), Err(e) => Err(Error::InternalError(GarageError::Message(format!( "{}: {}", - reason, e + reason.as_ref(), + e )))), } } } impl OkOrInternalError for Option { - type S2 = Result; - fn ok_or_internal_error(self, reason: &'static str) -> Result { + type S = T; + fn ok_or_internal_error>(self, reason: M) -> Result { match self { Some(x) => Ok(x), None => Err(Error::InternalError(GarageError::Message( - reason.to_string(), + reason.as_ref().to_string(), ))), } } -- cgit v1.2.3