I have followed the example provided here to customize the extractor error for the Json
extractor:
#[derive(FromRequest)]
#[from_request(via(axum::Json), rejection(ApiError))]
pub struct JsonExtractor<T>(pub T);
#[axum::debug_handler]
pub(crate) async fn create(
state: State<AppState>,
JsonExtractor(json): JsonExtractor<RouteBody>,
) -> Result<axum::Json<Customer>, ApiError>
This works great, but when I try the same for extracting from the Query, I get errors:
#[derive(FromRequest)]
#[from_request(via(axum::extract::Query), rejection(ApiError))]
pub struct QueryExtractor<T>(pub T);
#[axum::debug_handler]
pub(crate) async fn list(
state: State<AppState>,
query: QueryExtractor<RouteQuery>, // errors here
) -> Result<axum::Json<List<Customer>>, ApiError>
the trait bound `QueryExtractor<customers::list::RouteQuery>: FromRequest<AppState, hyper::Body, _>` is not satisfied
Function argument is not a valid axum extractor.
The difference is Json
implements FromRequest
while Query
implements FromRequestParts
(the latter doesn't consume the request body).
So you'd use #[derive(FromRequestParts)
instead:
This works similarly to
#[derive(FromRequest)]
except it usesFromRequestParts
. All the same options are supported.