diff options
author | Quentin <quentin@deuxfleurs.fr> | 2020-11-19 14:39:30 +0100 |
---|---|---|
committer | Quentin <quentin@deuxfleurs.fr> | 2020-11-19 14:39:30 +0100 |
commit | fc427b0b66241785ecd3fe23ec3e4ac72aaed1da (patch) | |
tree | ea406de04a7a11c09c6d758ca97ba53fc2bdfbe4 /src/api/error.rs | |
parent | 6076d869b14aa38059d54a2dece222ad7b9da3bc (diff) | |
parent | 044d7988779aadac9290b58ceb077c1211670aed (diff) | |
download | garage-fc427b0b66241785ecd3fe23ec3e4ac72aaed1da.tar.gz garage-fc427b0b66241785ecd3fe23ec3e4ac72aaed1da.zip |
Merge branch 'master' into feature/website
Diffstat (limited to 'src/api/error.rs')
-rw-r--r-- | src/api/error.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/api/error.rs b/src/api/error.rs new file mode 100644 index 00000000..ddb021db --- /dev/null +++ b/src/api/error.rs @@ -0,0 +1,116 @@ +use err_derive::Error; +use hyper::StatusCode; + +use garage_util::error::Error as GarageError; + +#[derive(Debug, Error)] +pub enum Error { + // Category: internal error + #[error(display = "Internal error: {}", _0)] + InternalError(#[error(source)] GarageError), + + #[error(display = "Internal error (Hyper error): {}", _0)] + Hyper(#[error(source)] hyper::Error), + + #[error(display = "Internal error (HTTP error): {}", _0)] + HTTP(#[error(source)] http::Error), + + // Category: cannot process + #[error(display = "Forbidden: {}", _0)] + Forbidden(String), + + #[error(display = "Not found")] + NotFound, + + // Category: bad request + #[error(display = "Invalid UTF-8: {}", _0)] + InvalidUTF8(#[error(source)] std::str::Utf8Error), + + #[error(display = "Invalid XML: {}", _0)] + InvalidXML(#[error(source)] roxmltree::Error), + + #[error(display = "Invalid header value: {}", _0)] + InvalidHeader(#[error(source)] hyper::header::ToStrError), + + #[error(display = "Invalid HTTP range: {:?}", _0)] + InvalidRange(#[error(from)] http_range::HttpRangeParseError), + + #[error(display = "Bad request: {}", _0)] + BadRequest(String), +} + +impl Error { + pub fn http_status_code(&self) -> StatusCode { + match self { + Error::NotFound => StatusCode::NOT_FOUND, + Error::Forbidden(_) => StatusCode::FORBIDDEN, + Error::InternalError(GarageError::RPC(_)) => StatusCode::SERVICE_UNAVAILABLE, + Error::InternalError(_) | Error::Hyper(_) | Error::HTTP(_) => { + StatusCode::INTERNAL_SERVER_ERROR + } + _ => StatusCode::BAD_REQUEST, + } + } +} + +pub trait OkOrBadRequest { + type S2; + fn ok_or_bad_request(self, reason: &'static str) -> Self::S2; +} + +impl<T, E> OkOrBadRequest for Result<T, E> +where + E: std::fmt::Display, +{ + type S2 = Result<T, Error>; + fn ok_or_bad_request(self, reason: &'static str) -> Result<T, Error> { + match self { + Ok(x) => Ok(x), + Err(e) => Err(Error::BadRequest(format!("{}: {}", reason, e))), + } + } +} + +impl<T> OkOrBadRequest for Option<T> { + type S2 = Result<T, Error>; + fn ok_or_bad_request(self, reason: &'static str) -> Result<T, Error> { + match self { + Some(x) => Ok(x), + None => Err(Error::BadRequest(format!("{}", reason))), + } + } +} + +pub trait OkOrInternalError { + type S2; + fn ok_or_internal_error(self, reason: &'static str) -> Self::S2; +} + +impl<T, E> OkOrInternalError for Result<T, E> +where + E: std::fmt::Display, +{ + type S2 = Result<T, Error>; + fn ok_or_internal_error(self, reason: &'static str) -> Result<T, Error> { + match self { + Ok(x) => Ok(x), + Err(e) => Err(Error::InternalError(GarageError::Message(format!( + "{}: {}", + reason, e + )))), + } + } +} + +impl<T> OkOrInternalError for Option<T> { + type S2 = Result<T, Error>; + fn ok_or_internal_error(self, reason: &'static str) -> Result<T, Error> { + match self { + Some(x) => Ok(x), + None => Err(Error::InternalError(GarageError::Message(format!( + "{}", + reason + )))), + } + } +} |