Skip to content

Commit

Permalink
Use JsonRejection::{status, body_text} in customize extractor error…
Browse files Browse the repository at this point in the history
… example (#1790)
  • Loading branch information
davidpdrsn authored Feb 25, 2023
1 parent 37e2a7d commit 1dc4b44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
10 changes: 2 additions & 8 deletions examples/customize-extractor-error/src/custom_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions examples/customize-extractor-error/src/derive_from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand All @@ -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()
}
}
22 changes: 11 additions & 11 deletions examples/customize-extractor-error/src/with_rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
}
}

0 comments on commit 1dc4b44

Please sign in to comment.