mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-25 08:37:29 +01:00
Add IntoResponse impl to customize-extractor-error example (#2191)
This commit is contained in:
parent
70171980cc
commit
368c3ee08f
1 changed files with 10 additions and 1 deletions
|
@ -15,10 +15,11 @@ use axum::{
|
|||
extract::rejection::JsonRejection, extract::FromRequest, http::StatusCode,
|
||||
response::IntoResponse,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub async fn handler(Json(value): Json<Value>) -> impl IntoResponse {
|
||||
Json(dbg!(value));
|
||||
Json(dbg!(value))
|
||||
}
|
||||
|
||||
// create an extractor that internally uses `axum::Json` but has a custom rejection
|
||||
|
@ -26,6 +27,14 @@ pub async fn handler(Json(value): Json<Value>) -> impl IntoResponse {
|
|||
#[from_request(via(axum::Json), rejection(ApiError))]
|
||||
pub struct Json<T>(T);
|
||||
|
||||
// We implement `IntoResponse` for our extractor so it can be used as a response
|
||||
impl<T: Serialize> IntoResponse for Json<T> {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let Self(value) = self;
|
||||
axum::Json(value).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
// We create our own rejection type
|
||||
#[derive(Debug)]
|
||||
pub struct ApiError {
|
||||
|
|
Loading…
Reference in a new issue