diff --git a/examples/customize-extractor-error/src/custom_extractor.rs b/examples/customize-extractor-error/src/custom_extractor.rs index d9093bc4..4351384b 100644 --- a/examples/customize-extractor-error/src/custom_extractor.rs +++ b/examples/customize-extractor-error/src/custom_extractor.rs @@ -51,18 +51,12 @@ where // convert the error from `axum::Json` into whatever we want Err(rejection) => { let payload = json!({ - "message": rejection.to_string(), + "message": rejection.body_text(), "origin": "custom_extractor", "path": path, }); - let code = match rejection { - 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))) + Err((rejection.status(), axum::Json(payload))) } } } diff --git a/examples/customize-extractor-error/src/derive_from_request.rs b/examples/customize-extractor-error/src/derive_from_request.rs index 762d602e..b8f7be90 100644 --- a/examples/customize-extractor-error/src/derive_from_request.rs +++ b/examples/customize-extractor-error/src/derive_from_request.rs @@ -27,22 +27,16 @@ pub struct Json<T>(T); // We create our own rejection type #[derive(Debug)] pub struct ApiError { - code: StatusCode, + status: StatusCode, message: String, } // We implement `From<JsonRejection> for ApiError` impl From<JsonRejection> for ApiError { 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 { - code, - message: rejection.to_string(), + status: rejection.status(), + message: rejection.body_text(), } } } @@ -55,6 +49,6 @@ impl IntoResponse for ApiError { "origin": "derive_from_request" }); - (self.code, axum::Json(payload)).into_response() + (self.status, axum::Json(payload)).into_response() } } diff --git a/examples/customize-extractor-error/src/with_rejection.rs b/examples/customize-extractor-error/src/with_rejection.rs index c7a98178..1916c369 100644 --- a/examples/customize-extractor-error/src/with_rejection.rs +++ b/examples/customize-extractor-error/src/with_rejection.rs @@ -13,7 +13,7 @@ //! [`thiserror`]: https://crates.io/crates/thiserror //! [#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 serde_json::{json, Value}; use thiserror::Error; @@ -37,21 +37,21 @@ pub enum ApiError { #[error(transparent)] JsonExtractorRejection(#[from] JsonRejection), } + // We implement `IntoResponse` so ApiError can be used as a response impl IntoResponse for ApiError { 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!({ - "message": self.to_string(), + "message": message, "origin": "with_rejection" }); - let code = match self { - ApiError::JsonExtractorRejection(x) => match x { - 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() + + (status, Json(payload)).into_response() } }