Add IntoResponse impl to customize-extractor-error example (#2191)

This commit is contained in:
Lennart Melzer 2023-09-11 14:12:03 +02:00 committed by GitHub
parent 70171980cc
commit 368c3ee08f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {