From 5f920d66cfbe6ce6349fecb475418e505a89d515 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Wed, 13 Jul 2022 11:07:07 +0200 Subject: [PATCH] chore(server): send error message in the json `error` field in response Previously it returned enum in json in `error_message` filed. --- types/src/errors/server.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/types/src/errors/server.rs b/types/src/errors/server.rs index f442a7e..fb2304a 100644 --- a/types/src/errors/server.rs +++ b/types/src/errors/server.rs @@ -7,9 +7,9 @@ use super::{AuthError, FsError}; #[derive(Debug, Clone, Serialize, Deserialize, Error)] #[serde(tag = "error", content = "error_message", rename_all = "kebab-case")] pub enum Error { - #[error("auth error - {0}")] + #[error("auth error: {0}")] AuthError(#[from] AuthError), - #[error("fs error - {0}")] + #[error("fs error: {0}")] FsError(#[from] FsError), #[error("too may requests, please slow down")] TooManyRequests, @@ -21,10 +21,22 @@ pub enum Error { JsonSyntaxError, #[error("failed to extract the request body")] BytesRejection, - #[error("other error - {0}")] + #[error("other error: {0}")] Other(String), } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +enum ResponseError { + Error(String) +} + +impl Error { + fn into_response(self) -> ResponseError { + ResponseError::Error(self.to_string()) + } +} + #[cfg(feature = "axum")] impl axum::response::IntoResponse for Error { fn into_response(self) -> axum::response::Response { @@ -51,7 +63,7 @@ impl axum::response::IntoResponse for Error { _ => StatusCode::BAD_REQUEST, }; - let mut response = axum::Json(self).into_response(); + let mut response = axum::Json(self.into_response()).into_response(); *response.status_mut() = status; response