Error handling example not displaying the correct behavior (#3788)

This commit is contained in:
Billard Philippe
2026-06-08 20:55:03 +02:00
committed by GitHub
parent f042e6de6e
commit e1f5f5eaa7
+7 -3
View File
@@ -58,6 +58,11 @@ async fn main() {
let app = Router::new() let app = Router::new()
// A dummy route that accepts some JSON but sometimes fails // A dummy route that accepts some JSON but sometimes fails
.route("/users", post(users_create)) .route("/users", post(users_create))
// Layers wraps from the inside out, each new .layer() call becomes the outer one.
// Putting TraceLayer outermost means its `request` span is entered before
// log_app_errors runs, so our tracing::error! events inherit that span and get
// prefixed with `request{method=… uri=… matched_path=…}:`.
.layer(from_fn(log_app_errors)) // <- Inner layer
.layer( .layer(
TraceLayer::new_for_http() TraceLayer::new_for_http()
// Create our own span for the request and include the matched path. The matched // Create our own span for the request and include the matched path. The matched
@@ -77,15 +82,14 @@ async fn main() {
// By default `TraceLayer` will log 5xx responses but we're doing our specific // By default `TraceLayer` will log 5xx responses but we're doing our specific
// logging of errors so disable that // logging of errors so disable that
.on_failure(()), .on_failure(()),
) ) // <- Outer layer
.layer(from_fn(log_app_errors))
.with_state(state); .with_state(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await .await
.unwrap(); .unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap()); tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await; let _ = axum::serve(listener, app).await;
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]