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
3 changed files with 17 additions and 29 deletions
@@ -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()
}
}