From 2a69b273cea3bea597db35e86e21049b80b6edd8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 15 Jan 2024 14:48:48 -0500 Subject: [PATCH] Use a standalone error type for `--find-links` registry (#936) --- crates/puffin-cli/src/commands/venv.rs | 2 +- crates/puffin-client/src/error.rs | 15 +++++---------- crates/puffin-client/src/flat_index.rs | 21 ++++++++++++++++----- crates/puffin-client/src/lib.rs | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/crates/puffin-cli/src/commands/venv.rs b/crates/puffin-cli/src/commands/venv.rs index 14dbf332c..6c7e0edfc 100644 --- a/crates/puffin-cli/src/commands/venv.rs +++ b/crates/puffin-cli/src/commands/venv.rs @@ -70,7 +70,7 @@ enum VenvError { #[error("Failed to resolve `--find-links` entry")] #[diagnostic(code(puffin::venv::flat_index))] - FlatIndexError(#[source] puffin_client::Error), + FlatIndexError(#[source] puffin_client::FlatIndexError), } /// Create a virtual environment. diff --git a/crates/puffin-client/src/error.rs b/crates/puffin-client/src/error.rs index cb61e310c..a6bd472f2 100644 --- a/crates/puffin-client/src/error.rs +++ b/crates/puffin-client/src/error.rs @@ -1,15 +1,13 @@ -use std::io; - use async_http_range_reader::AsyncHttpRangeReaderError; use async_zip::error::ZipError; -use thiserror::Error; use url::Url; -use crate::html; use distribution_filename::{WheelFilename, WheelFilenameError}; use puffin_normalize::PackageName; -#[derive(Debug, Error)] +use crate::html; + +#[derive(Debug, thiserror::Error)] pub enum Error { /// An invalid URL was provided. #[error(transparent)] @@ -72,10 +70,10 @@ pub enum Error { Zip(WheelFilename, #[source] ZipError), #[error("Failed to write to the client cache")] - CacheWrite(#[source] io::Error), + CacheWrite(#[source] std::io::Error), #[error(transparent)] - Io(#[from] io::Error), + Io(#[from] std::io::Error), #[error("Cache deserialization failed")] Decode(#[from] rmp_serde::decode::Error), @@ -95,9 +93,6 @@ pub enum Error { #[error("Unsupported `Content-Type` \"{1}\" for {0}. Expected JSON or HTML.")] UnsupportedMediaType(Url, String), - - #[error("Failed to read find links directory")] - FindLinks(#[source] io::Error), } impl Error { diff --git a/crates/puffin-client/src/flat_index.rs b/crates/puffin-client/src/flat_index.rs index 8de22ab66..427abd3f8 100644 --- a/crates/puffin-client/src/flat_index.rs +++ b/crates/puffin-client/src/flat_index.rs @@ -22,6 +22,15 @@ use pypi_types::Hashes; use crate::html::SimpleHtml; use crate::{Error, RegistryClient}; +#[derive(Debug, thiserror::Error)] +pub enum FlatIndexError { + #[error("Failed to read `--find-links` directory: {0}")] + FindLinksDirectory(PathBuf, #[source] std::io::Error), + + #[error("Failed to read `--find-links` URL: {0}")] + FindLinksUrl(Url, #[source] Error), +} + type FlatIndexEntry = (DistFilename, File, IndexUrl); /// A client for reading distributions from `--find-links` entries (either local directories or @@ -43,15 +52,17 @@ impl<'a> FlatIndexClient<'a> { pub async fn fetch( &self, indexes: impl Iterator, - ) -> Result, Error> { + ) -> Result, FlatIndexError> { let mut dists = Vec::new(); // TODO(konstin): Parallelize reads over flat indexes. for flat_index in indexes { let index_dists = match flat_index { - FlatIndexLocation::Path(path) => { - Self::read_from_directory(path).map_err(Error::FindLinks)? - } - FlatIndexLocation::Url(url) => self.read_from_url(url).await?, + FlatIndexLocation::Path(path) => Self::read_from_directory(path) + .map_err(|err| FlatIndexError::FindLinksDirectory(path.clone(), err))?, + FlatIndexLocation::Url(url) => self + .read_from_url(url) + .await + .map_err(|err| FlatIndexError::FindLinksUrl(url.clone(), err))?, }; if index_dists.is_empty() { warn!("No packages found in `--find-links` entry: {}", flat_index); diff --git a/crates/puffin-client/src/lib.rs b/crates/puffin-client/src/lib.rs index 613c7c685..c25e68d28 100644 --- a/crates/puffin-client/src/lib.rs +++ b/crates/puffin-client/src/lib.rs @@ -1,6 +1,6 @@ pub use cached_client::{CachedClient, CachedClientError, DataWithCachePolicy}; pub use error::Error; -pub use flat_index::{FlatDistributions, FlatIndex, FlatIndexClient}; +pub use flat_index::{FlatDistributions, FlatIndex, FlatIndexClient, FlatIndexError}; pub use registry_client::{ read_metadata_async, RegistryClient, RegistryClientBuilder, SimpleMetadata, VersionFiles, };