From 368c3ee08fc3896358d3bd2bfc8cc67f2925c6ef Mon Sep 17 00:00:00 2001 From: Lennart Melzer Date: Mon, 11 Sep 2023 14:12:03 +0200 Subject: [PATCH] Add IntoResponse impl to customize-extractor-error example (#2191) --- .../src/derive_from_request.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/customize-extractor-error/src/derive_from_request.rs b/examples/customize-extractor-error/src/derive_from_request.rs index 7295823e..84fd9bbf 100644 --- a/examples/customize-extractor-error/src/derive_from_request.rs +++ b/examples/customize-extractor-error/src/derive_from_request.rs @@ -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) -> 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) -> impl IntoResponse { #[from_request(via(axum::Json), rejection(ApiError))] pub struct Json(T); +// We implement `IntoResponse` for our extractor so it can be used as a response +impl IntoResponse for Json { + 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 {