tower-rs / tower-http

HTTP specific Tower utilities.
680 stars 159 forks source link

`ServeDir::try_call` does not propagate result as documented #412

Open Pistonight opened 11 months ago

Pistonight commented 11 months ago

Bug Report

Version

tower-http v0.4.4

Platform

All

Description

When using ServeDir::try_call to request a path that does not exist, it returns Ok(404) instead of an Err

use axum::http::Request;
use tower_http::services::ServeDir;
use tracing::{error, info};

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt::init();

    let mut serve_dir = ServeDir::new("src");

    let mut req = Request::new(());
    *req.uri_mut() = "/main.rs".parse().unwrap();
    match serve_dir.try_call(req).await {
        Ok(response) => info!("response code: {}", response.status()),
        Err(e) => error!("error: {}", e),
    }

    let mut req2 = Request::new(());
    *req2.uri_mut() = "/doesnotexist.rs".parse().unwrap();
    match serve_dir.try_call(req2).await {
        Ok(response) => info!("response code: {}", response.status()),
        Err(e) => error!("error: {}", e),
    }
}

Expected:

2023-09-23T18:09:09.141584Z  INFO rs_playground: response code: 200 OK
2023-09-23T18:09:09.141801Z  ERROR rs_playground: error: <something>

Actual:

2023-09-23T18:09:09.141584Z  INFO rs_playground: response code: 200 OK
2023-09-23T18:09:09.141801Z  INFO rs_playground: response code: 404 Not Found
jplatte commented 10 months ago

I think a PR to fix this would be welcome (cc @davidpdrsn).