From 55bf15513a16b104e0138681fbe8b0a8f7557513 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sun, 8 Mar 2026 08:02:23 +0100 Subject: [PATCH] json download: return status 400 instead of server error for invalid format version --- .../bin/docs_rs_web/src/handlers/rustdoc.rs | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/bin/docs_rs_web/src/handlers/rustdoc.rs b/crates/bin/docs_rs_web/src/handlers/rustdoc.rs index bb3b4f18e..22d9cf93e 100644 --- a/crates/bin/docs_rs_web/src/handlers/rustdoc.rs +++ b/crates/bin/docs_rs_web/src/handlers/rustdoc.rs @@ -933,7 +933,8 @@ pub(crate) async fn json_download_handler( stripped_format_version .parse::() - .context("can't parse format version")? + .context("can't parse format version") + .map_err(AxumNope::BadRequest)? } else { RustdocJsonFormatVersion::Latest }; @@ -1074,7 +1075,10 @@ mod test { RUSTDOC_JSON_COMPRESSION_ALGORITHMS, read_format_version_from_rustdoc_json, }; use docs_rs_storage::{decompress, testing::check_archive_consistency}; - use docs_rs_types::Version; + use docs_rs_types::{ + Version, + testing::{KRATE, V2}, + }; use docs_rs_uri::encode_url_path; use kuchikiki::traits::TendrilSink; use pretty_assertions::assert_eq; @@ -3639,6 +3643,34 @@ mod test { Ok(()) } + #[tokio::test(flavor = "multi_thread")] + async fn json_download_bad_request() -> Result<()> { + let env = TestEnvironment::new().await?; + + env.fake_release() + .await + .name(KRATE) + .version(V2) + .archive_storage(true) + .default_target("x86_64-unknown-linux-gnu") + .create() + .await?; + + let web = env.web_app().await; + + let response = web + .get(&format!("/crate/{KRATE}/{V2}/json/963570%40")) + .await?; + assert_eq!(response.status(), StatusCode::BAD_REQUEST); + assert!( + response + .text() + .await? + .contains("can't parse format version") + ); + Ok(()) + } + #[test_case("0.1.0/json"; "rustdoc status false")] #[test_case("0.2.0/unknown-target/json"; "unknown target")] #[test_case("0.2.0/json/99"; "target file doesnt exist")]