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