-
SummaryI would like to be able to access the incoming request in fallback and error handlers. For example, Router::new()
.method_not_allowed_fallback(|| async { todo!("I want to get the request method in here!") })
.layer(CatchPanicLayer::custom(|error| -> Response<i64> {
todo!("I want to access the x-request-id header here!")
})) How can I do this? axum version0.7.9 |
Beta Was this translation helpful? Give feedback.
Answered by
jplatte
Dec 25, 2024
Replies: 2 comments 3 replies
-
You should be able to use all extractors (including |
Beta Was this translation helpful? Give feedback.
2 replies
-
Something like this should work: use axum::{
extract::Request,
middleware::{self, Next},
};
use futures_util::FutureExt as _;
Router::new()
.method_not_allowed_fallback(|method: http::Method| async move {
dbg!(method);
http::StatusCode::METHOD_NOT_ALLOWED.into_response()
})
.layer(middleware::from_fn(|request: Request, next: Next| async move {
let req_id = request.headers().get("x-request-id").cloned();
match next.run(request).catch_unwind().await {
Ok(response) => response,
Err(panic_payload) => {
dbg!(panic_payload);
http::StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
})) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
FelixZY
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this should work: