mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-29 17:50:27 +01:00
59 lines
2.1 KiB
Rust
59 lines
2.1 KiB
Rust
//! Manual implementation of `FromRequest` that wraps another extractor
|
|
//!
|
|
//! + Powerful API: Implementing `FromRequest` grants access to `RequestParts`
|
|
//! and `async/await`. This means that you can create more powerful rejections
|
|
//! - Boilerplate: Requires creating a new extractor for every custom rejection
|
|
//! - Complexity: Manually implementing `FromRequest` results on more complex code
|
|
use axum::{
|
|
extract::{rejection::JsonRejection, FromRequest, MatchedPath, Request},
|
|
http::StatusCode,
|
|
response::IntoResponse,
|
|
RequestPartsExt,
|
|
};
|
|
use serde_json::{json, Value};
|
|
|
|
pub async fn handler(Json(value): Json<Value>) -> impl IntoResponse {
|
|
Json(dbg!(value));
|
|
}
|
|
|
|
// We define our own `Json` extractor that customizes the error from `axum::Json`
|
|
pub struct Json<T>(pub T);
|
|
|
|
impl<S, T> FromRequest<S> for Json<T>
|
|
where
|
|
axum::Json<T>: FromRequest<S, Rejection = JsonRejection>,
|
|
S: Send + Sync,
|
|
{
|
|
type Rejection = (StatusCode, axum::Json<Value>);
|
|
|
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
|
let (mut parts, body) = req.into_parts();
|
|
|
|
// We can use other extractors to provide better rejection messages.
|
|
// For example, here we are using `axum::extract::MatchedPath` to
|
|
// provide a better error message.
|
|
//
|
|
// Have to run that first since `Json` extraction consumes the request.
|
|
let path = parts
|
|
.extract::<MatchedPath>()
|
|
.await
|
|
.map(|path| path.as_str().to_owned())
|
|
.ok();
|
|
|
|
let req = Request::from_parts(parts, body);
|
|
|
|
match axum::Json::<T>::from_request(req, state).await {
|
|
Ok(value) => Ok(Self(value.0)),
|
|
// convert the error from `axum::Json` into whatever we want
|
|
Err(rejection) => {
|
|
let payload = json!({
|
|
"message": rejection.body_text(),
|
|
"origin": "custom_extractor",
|
|
"path": path,
|
|
});
|
|
|
|
Err((rejection.status(), axum::Json(payload)))
|
|
}
|
|
}
|
|
}
|
|
}
|