From 43b7eb76b65b84277dc384e2d6dad7d3b030dcfe Mon Sep 17 00:00:00 2001 From: konstin Date: Mon, 27 Oct 2025 11:23:53 +0100 Subject: [PATCH] Review: Separate error types --- crates/uv-client/src/error.rs | 17 ++++++++++------- crates/uv-client/src/registry_client.rs | 10 ++++++---- crates/uv-publish/src/lib.rs | 10 ++-------- crates/uv-resolver/src/resolver/provider.rs | 3 ++- crates/uv/src/commands/pip/latest.rs | 7 ++++--- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/crates/uv-client/src/error.rs b/crates/uv-client/src/error.rs index ddc2b8556..b2ec2ef8f 100644 --- a/crates/uv-client/src/error.rs +++ b/crates/uv-client/src/error.rs @@ -266,16 +266,19 @@ pub enum ErrorKind { #[error("{0} isn't available locally, but making network requests to registries was banned")] NoIndex(String), - /// The package was not found in the registry. + /// The package was not found in the registry (HTTP Error 404). /// /// Make sure the package name is spelled correctly and that you've /// configured the right registry to fetch it from. - #[error("Package `{package_name}` was not found in the registry")] - PackageNotFound { - package_name: String, - /// Not mentioned in the message, there's a separate hint with the details. - status_code_error: bool, - }, + #[error("Package `{0}` was not found in the registry")] + PackageNotFound(PackageName), + + /// The package was not found in the registry (HTTP Error 401 or 403). + /// + /// Make sure the package name is spelled correctly and that you've + /// configured the right registry to fetch it from. + #[error("Package `{0}` was not found in the registry")] + StatusCodeError(PackageName), /// The package was not found in the local (file-based) index. #[error("Package `{0}` was not found in the local index")] diff --git a/crates/uv-client/src/registry_client.rs b/crates/uv-client/src/registry_client.rs index 495108285..3c9b476b1 100644 --- a/crates/uv-client/src/registry_client.rs +++ b/crates/uv-client/src/registry_client.rs @@ -421,11 +421,13 @@ impl RegistryClient { if results.is_empty() { return match self.connectivity { - Connectivity::Online => Err(ErrorKind::PackageNotFound { - package_name: package_name.to_string(), - status_code_error: any_status_code_error.load(Ordering::Relaxed), + Connectivity::Online => { + if any_status_code_error.load(Ordering::Relaxed) { + Err(ErrorKind::StatusCodeError(package_name.clone()).into()) + } else { + Err(ErrorKind::PackageNotFound(package_name.clone()).into()) + } } - .into()), Connectivity::Offline => Err(ErrorKind::Offline(package_name.to_string()).into()), }; } diff --git a/crates/uv-publish/src/lib.rs b/crates/uv-publish/src/lib.rs index 12ba67f51..abedd1f2f 100644 --- a/crates/uv-publish/src/lib.rs +++ b/crates/uv-publish/src/lib.rs @@ -556,20 +556,14 @@ pub async fn check_url( Ok(response) => response, Err(err) => { return match err.kind() { - uv_client::ErrorKind::PackageNotFound { - status_code_error: false, - .. - } => { + uv_client::ErrorKind::PackageNotFound(_) => { // The package doesn't exist, it's the first upload of the package. warn!( "Package not found in the registry; skipping upload check for {filename}" ); Ok(false) } - uv_client::ErrorKind::PackageNotFound { - status_code_error: true, - .. - } => { + uv_client::ErrorKind::StatusCodeError(_) => { // The package may or may not exist, there was an authentication failure. // TODO(konsti): We should show the real index error instead. let status_code_detail = if index_capabilities.unauthorized(index_url) { diff --git a/crates/uv-resolver/src/resolver/provider.rs b/crates/uv-resolver/src/resolver/provider.rs index 3e2744a6c..7566b0f04 100644 --- a/crates/uv-resolver/src/resolver/provider.rs +++ b/crates/uv-resolver/src/resolver/provider.rs @@ -200,7 +200,8 @@ impl ResolverProvider for DefaultResolverProvider<'_, Con .collect(), )), Err(err) => match err.kind() { - uv_client::ErrorKind::PackageNotFound { .. } => { + uv_client::ErrorKind::PackageNotFound(_) + | uv_client::ErrorKind::StatusCodeError(_) => { if let Some(flat_index) = flat_index .and_then(|flat_index| flat_index.get(package_name)) .cloned() diff --git a/crates/uv/src/commands/pip/latest.rs b/crates/uv/src/commands/pip/latest.rs index 1ee6ad870..09d53db91 100644 --- a/crates/uv/src/commands/pip/latest.rs +++ b/crates/uv/src/commands/pip/latest.rs @@ -46,9 +46,10 @@ impl LatestClient<'_> { Ok(archives) => archives, Err(err) => { return match err.kind() { - uv_client::ErrorKind::PackageNotFound { .. } => Ok(None), - uv_client::ErrorKind::NoIndex(_) => Ok(None), - uv_client::ErrorKind::Offline(_) => Ok(None), + uv_client::ErrorKind::PackageNotFound(_) + | uv_client::ErrorKind::StatusCodeError(_) + | uv_client::ErrorKind::NoIndex(_) + | uv_client::ErrorKind::Offline(_) => Ok(None), _ => Err(err), }; }