Attribute Macro axum_macros::debug_middleware

source ·
#[debug_middleware]
Expand description

Generates better error messages when applied to middleware functions.

This works similarly to #[debug_handler] except for middleware using axum::middleware::from_fn.

§Example

use axum::{
    routing::get,
    extract::Request,
    response::Response,
    Router,
    middleware::{self, Next},
    debug_middleware,
};

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(|| async {}))
        .layer(middleware::from_fn(my_middleware));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

// if this wasn't a valid middleware function #[debug_middleware] would
// improve compile error
#[debug_middleware]
async fn my_middleware(
    request: Request,
    next: Next,
) -> Response {
    next.run(request).await
}

§Performance

This macro has no effect when compiled with the release profile. (eg. cargo build --release)