Crate axum_debug[][src]

Expand description

This is a debugging crate that provides better error messages for axum framework.

While using axum, you can get long error messages for simple mistakes. For example:

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

fn handler() -> &'static str {
    "Hello, world"
}

You will get a long error message about function not implementing Handler trait. But why this function does not implement it? To figure it out debug_handler macro can be used.

#[debug_handler]
fn handler() -> &'static str {
    "Hello, world"
}
error: handlers must be async functions
  --> main.rs:xx:1
   |
xx | fn handler() -> &'static str {
   | ^^

As the error message says, handler function needs to be async.

use axum::{routing::get, Router};
use axum_debug::debug_handler;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

#[debug_handler]
async fn handler() -> &'static str {
    "Hello, world"
}

Performance

Macros in this crate have no effect when using release profile. (eg. cargo build --release)

Attribute Macros

Generates better error messages when applied to a handler function.