Skip to content

handle_error increases compiles a lot #145

@davidpdrsn

Description

@davidpdrsn

It seems adding handle_error to a router will increase compile times quite significantly.

Here is an example:

use axum::{service::ServiceExt, prelude::*, http::StatusCode};
use std::convert::Infallible;
use std::net::SocketAddr;
use std::time::Duration;
use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;

#[tokio::main]
async fn main() {
    let app = route("/", get(handler))
        .nest(
            "/api",
            route("/pay/get_pay_params", post(handler))
                .nest(
                    "/user",
                    route("/create", post(handler))
                        .route("/login", post(handler))
                        .route("/info", get(handler))
                        .route("/update_password", get(handler)),
                )
                .nest(
                    "/product",
                    route("/list", get(handler)).route("/detail", get(handler)),
                ),
        )
        .layer(
            ServiceBuilder::new()
                .timeout(Duration::from_secs(10))
                .layer(TraceLayer::new_for_http())
                .into_inner(),
        )
        .handle_error(|error: BoxError| {
            if error.is::<tower::timeout::error::Elapsed>() {
                Ok::<_, Infallible>((
                    StatusCode::REQUEST_TIMEOUT,
                    "request took too long".to_string(),
                ))
            } else {
                Ok::<_, Infallible>((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Unhandled internal error: {}", error),
                ))
            }
        });

    let addr: SocketAddr = "127.0.0.1".parse::<SocketAddr>().unwrap();
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn handler() -> &'static str {
    "index"
}

With the handle_error it compiles in 5s and in 0.3s without, on my machine in debug mode. Whether this is a rustc issue or something we can improve directly via axum I don't know. Something to investigate for sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions