Skip to content

Commit

Permalink
Use a standalone error type for --find-links registry (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jan 15, 2024
1 parent e71e3e8 commit 2a69b27
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/puffin-cli/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 5 additions & 10 deletions crates/puffin-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions crates/puffin-client/src/flat_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,15 +52,17 @@ impl<'a> FlatIndexClient<'a> {
pub async fn fetch(
&self,
indexes: impl Iterator<Item = &FlatIndexLocation>,
) -> Result<Vec<FlatIndexEntry>, Error> {
) -> Result<Vec<FlatIndexEntry>, 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);
Expand Down
2 changes: 1 addition & 1 deletion crates/puffin-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down

0 comments on commit 2a69b27

Please sign in to comment.