Error refactoring

This commit is contained in:
Alex Auvolat
2022-05-13 15:04:53 +02:00
parent c0fb9fd0fe
commit 96b11524d5
24 changed files with 135 additions and 206 deletions
+9 -8
View File
@@ -1,12 +1,8 @@
use err_derive::Error;
use hyper::header::HeaderValue;
use hyper::{Body, HeaderMap, StatusCode};
use hyper::StatusCode;
use garage_model::helper::error::Error as HelperError;
use garage_util::error::Error as GarageError;
use crate::generic_server::ApiError;
/// Errors of this crate
#[derive(Debug, Error)]
pub enum CommonError {
@@ -36,8 +32,9 @@ impl CommonError {
| GarageError::RemoteError(_)
| GarageError::Quorum(_, _, _, _),
) => StatusCode::SERVICE_UNAVAILABLE,
CommonError::InternalError(_) | CommonError::Hyper(_) | CommonError::Http(_) =>
StatusCode::INTERNAL_SERVER_ERROR,
CommonError::InternalError(_) | CommonError::Hyper(_) | CommonError::Http(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
CommonError::BadRequest(_) => StatusCode::BAD_REQUEST,
}
}
@@ -57,7 +54,11 @@ where
fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<T, CommonError> {
match self {
Ok(x) => Ok(x),
Err(e) => Err(CommonError::BadRequest(format!("{}: {}", reason.as_ref(), e))),
Err(e) => Err(CommonError::BadRequest(format!(
"{}: {}",
reason.as_ref(),
e
))),
}
}
}