Json body for custom errors
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Alex 2022-05-13 19:28:23 +02:00
parent ea325d78d3
commit 5a535788fc
Signed by: lx
GPG key ID: 0E496D15096376BE
3 changed files with 67 additions and 11 deletions

View file

@ -7,6 +7,7 @@ use garage_model::helper::error::Error as HelperError;
use crate::common_error::CommonError; use crate::common_error::CommonError;
pub use crate::common_error::{CommonErrorDerivative, OkOrBadRequest, OkOrInternalError}; pub use crate::common_error::{CommonErrorDerivative, OkOrBadRequest, OkOrInternalError};
use crate::generic_server::ApiError; use crate::generic_server::ApiError;
use crate::helpers::CustomApiErrorBody;
/// Errors of this crate /// Errors of this crate
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -44,6 +45,15 @@ impl From<HelperError> for Error {
} }
} }
impl Error {
fn code(&self) -> &'static str {
match self {
Error::CommonError(c) => c.aws_code(),
Error::NoSuchAccessKey => "NoSuchAccessKey",
}
}
}
impl ApiError for Error { impl ApiError for Error {
/// Get the HTTP status code that best represents the meaning of the error for the client /// Get the HTTP status code that best represents the meaning of the error for the client
fn http_status_code(&self) -> StatusCode { fn http_status_code(&self) -> StatusCode {
@ -58,10 +68,20 @@ impl ApiError for Error {
} }
fn http_body(&self, garage_region: &str, path: &str) -> Body { fn http_body(&self, garage_region: &str, path: &str) -> Body {
// TODO nice json error let error = CustomApiErrorBody {
Body::from(format!( code: self.code().to_string(),
"ERROR: {}\n\ngarage region: {}\npath: {}", message: format!("{}", self),
self, garage_region, path path: path.to_string(),
)) region: garage_region.to_string(),
};
Body::from(serde_json::to_string_pretty(&error).unwrap_or_else(|_| {
r#"
{
"code": "InternalError",
"message": "JSON encoding of error failed"
}
"#
.into()
}))
} }
} }

View file

@ -1,6 +1,6 @@
use hyper::{Body, Request}; use hyper::{Body, Request};
use idna::domain_to_unicode; use idna::domain_to_unicode;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use crate::common_error::{CommonError as Error, *}; use crate::common_error::{CommonError as Error, *};
@ -279,3 +279,11 @@ mod tests {
); );
} }
} }
#[derive(Serialize)]
pub(crate) struct CustomApiErrorBody {
pub(crate) code: String,
pub(crate) message: String,
pub(crate) region: String,
pub(crate) path: String,
}

View file

@ -7,6 +7,7 @@ use garage_model::helper::error::Error as HelperError;
use crate::common_error::CommonError; use crate::common_error::CommonError;
pub use crate::common_error::{CommonErrorDerivative, OkOrBadRequest, OkOrInternalError}; pub use crate::common_error::{CommonErrorDerivative, OkOrBadRequest, OkOrInternalError};
use crate::generic_server::ApiError; use crate::generic_server::ApiError;
use crate::helpers::CustomApiErrorBody;
use crate::signature::error::Error as SignatureError; use crate::signature::error::Error as SignatureError;
/// Errors of this crate /// Errors of this crate
@ -78,6 +79,23 @@ impl From<SignatureError> for Error {
} }
} }
impl Error {
/// This returns a keyword for the corresponding error.
/// Here, these keywords are not necessarily those from AWS S3,
/// as we are building a custom API
fn code(&self) -> &'static str {
match self {
Error::CommonError(c) => c.aws_code(),
Error::NoSuchKey => "NoSuchKey",
Error::NotAcceptable(_) => "NotAcceptable",
Error::AuthorizationHeaderMalformed(_) => "AuthorizationHeaderMalformed",
Error::InvalidBase64(_) => "InvalidBase64",
Error::InvalidHeader(_) => "InvalidHeaderValue",
Error::InvalidUtf8Str(_) => "InvalidUtf8String",
}
}
}
impl ApiError for Error { impl ApiError for Error {
/// Get the HTTP status code that best represents the meaning of the error for the client /// Get the HTTP status code that best represents the meaning of the error for the client
fn http_status_code(&self) -> StatusCode { fn http_status_code(&self) -> StatusCode {
@ -97,10 +115,20 @@ impl ApiError for Error {
} }
fn http_body(&self, garage_region: &str, path: &str) -> Body { fn http_body(&self, garage_region: &str, path: &str) -> Body {
// TODO nice json error let error = CustomApiErrorBody {
Body::from(format!( code: self.code().to_string(),
"ERROR: {}\n\ngarage region: {}\npath: {}", message: format!("{}", self),
self, garage_region, path path: path.to_string(),
)) region: garage_region.to_string(),
};
Body::from(serde_json::to_string_pretty(&error).unwrap_or_else(|_| {
r#"
{
"code": "InternalError",
"message": "JSON encoding of error failed"
}
"#
.into()
}))
} }
} }