aboutsummaryrefslogtreecommitdiff
path: root/src/api/admin/error.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-05-13 19:28:23 +0200
committerAlex Auvolat <alex@adnab.me>2022-05-13 19:28:23 +0200
commit5a535788fc0a69950bbfdc6f189597c5e37a6e3b (patch)
tree765ea663ac5a393d000676a4886c5227d562efe1 /src/api/admin/error.rs
parentea325d78d36d19f59a0849ace1f4567e2b095bd7 (diff)
downloadgarage-5a535788fc0a69950bbfdc6f189597c5e37a6e3b.tar.gz
garage-5a535788fc0a69950bbfdc6f189597c5e37a6e3b.zip
Json body for custom errors
Diffstat (limited to 'src/api/admin/error.rs')
-rw-r--r--src/api/admin/error.rs30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/api/admin/error.rs b/src/api/admin/error.rs
index bb35c16b..38dfe5b6 100644
--- a/src/api/admin/error.rs
+++ b/src/api/admin/error.rs
@@ -7,6 +7,7 @@ use garage_model::helper::error::Error as HelperError;
use crate::common_error::CommonError;
pub use crate::common_error::{CommonErrorDerivative, OkOrBadRequest, OkOrInternalError};
use crate::generic_server::ApiError;
+use crate::helpers::CustomApiErrorBody;
/// Errors of this crate
#[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 {
/// Get the HTTP status code that best represents the meaning of the error for the client
fn http_status_code(&self) -> StatusCode {
@@ -58,10 +68,20 @@ impl ApiError for Error {
}
fn http_body(&self, garage_region: &str, path: &str) -> Body {
- // TODO nice json error
- Body::from(format!(
- "ERROR: {}\n\ngarage region: {}\npath: {}",
- self, garage_region, path
- ))
+ let error = CustomApiErrorBody {
+ code: self.code().to_string(),
+ message: format!("{}", self),
+ 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()
+ }))
}
}