Skip to content

Commit

Permalink
Add IntoResponse impl to customize-extractor-error example (#2191)
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart authored Sep 11, 2023
1 parent 7017198 commit 368c3ee
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/customize-extractor-error/src/derive_from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ 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
#[derive(FromRequest)]
#[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 {
Expand Down

0 comments on commit 368c3ee

Please sign in to comment.