1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-03-28 09:17:24 +01:00

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

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
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)))
}
}
}

View file

@ -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()
}
}

View file

@ -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()
}
}