Use JsonRejection::{status, body_text} in customize extractor error example (#1790)

This commit is contained in:
David Pedersen 2023-02-25 15:02:02 +01:00 committed by GitHub
parent 37e2a7d5e7
commit 1dc4b44472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 29 deletions

View File

@ -51,18 +51,12 @@ where
// convert the error from `axum::Json` into whatever we want // convert the error from `axum::Json` into whatever we want
Err(rejection) => { Err(rejection) => {
let payload = json!({ let payload = json!({
"message": rejection.to_string(), "message": rejection.body_text(),
"origin": "custom_extractor", "origin": "custom_extractor",
"path": path, "path": path,
}); });
let code = match rejection { Err((rejection.status(), axum::Json(payload)))
JsonRejection::JsonDataError(_) => StatusCode::UNPROCESSABLE_ENTITY,
JsonRejection::JsonSyntaxError(_) => StatusCode::BAD_REQUEST,
JsonRejection::MissingJsonContentType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
Err((code, axum::Json(payload)))
} }
} }
} }

View File

@ -27,22 +27,16 @@ pub struct Json<T>(T);
// We create our own rejection type // We create our own rejection type
#[derive(Debug)] #[derive(Debug)]
pub struct ApiError { pub struct ApiError {
code: StatusCode, status: StatusCode,
message: String, message: String,
} }
// We implement `From<JsonRejection> for ApiError` // We implement `From<JsonRejection> for ApiError`
impl From<JsonRejection> for ApiError { impl From<JsonRejection> for ApiError {
fn from(rejection: JsonRejection) -> Self { fn from(rejection: JsonRejection) -> Self {
let code = match rejection {
JsonRejection::JsonDataError(_) => StatusCode::UNPROCESSABLE_ENTITY,
JsonRejection::JsonSyntaxError(_) => StatusCode::BAD_REQUEST,
JsonRejection::MissingJsonContentType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
Self { Self {
code, status: rejection.status(),
message: rejection.to_string(), message: rejection.body_text(),
} }
} }
} }
@ -55,6 +49,6 @@ impl IntoResponse for ApiError {
"origin": "derive_from_request" "origin": "derive_from_request"
}); });
(self.code, axum::Json(payload)).into_response() (self.status, axum::Json(payload)).into_response()
} }
} }

View File

@ -13,7 +13,7 @@
//! [`thiserror`]: https://crates.io/crates/thiserror //! [`thiserror`]: https://crates.io/crates/thiserror
//! [#1116]: https://github.com/tokio-rs/axum/issues/1116#issuecomment-1186197684 //! [#1116]: https://github.com/tokio-rs/axum/issues/1116#issuecomment-1186197684
use axum::{extract::rejection::JsonRejection, http::StatusCode, response::IntoResponse, Json}; use axum::{extract::rejection::JsonRejection, response::IntoResponse, Json};
use axum_extra::extract::WithRejection; use axum_extra::extract::WithRejection;
use serde_json::{json, Value}; use serde_json::{json, Value};
use thiserror::Error; use thiserror::Error;
@ -37,21 +37,21 @@ pub enum ApiError {
#[error(transparent)] #[error(transparent)]
JsonExtractorRejection(#[from] JsonRejection), JsonExtractorRejection(#[from] JsonRejection),
} }
// We implement `IntoResponse` so ApiError can be used as a response // We implement `IntoResponse` so ApiError can be used as a response
impl IntoResponse for ApiError { impl IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
let (status, message) = match self {
ApiError::JsonExtractorRejection(json_rejection) => {
(json_rejection.status(), json_rejection.body_text())
}
};
let payload = json!({ let payload = json!({
"message": self.to_string(), "message": message,
"origin": "with_rejection" "origin": "with_rejection"
}); });
let code = match self {
ApiError::JsonExtractorRejection(x) => match x { (status, Json(payload)).into_response()
JsonRejection::JsonDataError(_) => StatusCode::UNPROCESSABLE_ENTITY,
JsonRejection::JsonSyntaxError(_) => StatusCode::BAD_REQUEST,
JsonRejection::MissingJsonContentType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
};
(code, Json(payload)).into_response()
} }
} }