Improve client reqwest errors (#276)

I had to debug a failure involving these errors and had to improve their
output.
This commit is contained in:
konsti 2023-11-01 16:52:58 +01:00 committed by GitHub
parent 997228f4be
commit d1af90163b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 11 deletions

View file

@ -126,6 +126,7 @@ impl RegistryClientBuilder {
pub struct RegistryClient {
pub(crate) index: Url,
pub(crate) extra_index: Vec<Url>,
/// Ignore the package index, instead relying on local archives and caches.
pub(crate) no_index: bool,
pub(crate) proxy: Url,
pub(crate) client: ClientWithMiddleware,
@ -136,7 +137,7 @@ impl RegistryClient {
/// Fetch a package from the `PyPI` simple API.
pub async fn simple(&self, package_name: impl AsRef<str>) -> Result<SimpleJson, Error> {
if self.no_index {
return Err(Error::PackageNotFound(package_name.as_ref().to_string()));
return Err(Error::NoIndex(package_name.as_ref().to_string()));
}
for index in std::iter::once(&self.index).chain(self.extra_index.iter()) {
@ -187,7 +188,7 @@ impl RegistryClient {
/// Fetch the metadata from a wheel file.
pub async fn file(&self, file: File) -> Result<Metadata21, Error> {
if self.no_index {
return Err(Error::FileNotFound(file.filename));
return Err(Error::NoIndex(file.filename));
}
// Per PEP 658, if `data-dist-info-metadata` is available, we can request it directly;
@ -203,7 +204,7 @@ impl RegistryClient {
// Fetch from the index.
let text = self.file_impl(&url).await.map_err(|err| {
if err.status() == Some(StatusCode::NOT_FOUND) {
Error::FileNotFound(file.filename.to_string())
Error::FileNotFound(file.filename, err)
} else {
err.into()
}
@ -228,7 +229,7 @@ impl RegistryClient {
url: &Url,
) -> Result<Box<dyn AsyncRead + Unpin + Send + Sync>, Error> {
if self.no_index {
return Err(Error::ResourceNotFound(url.clone()));
return Err(Error::NoIndex(url.to_string()));
}
Ok(Box::new(

View file

@ -1,5 +1,4 @@
use thiserror::Error;
use url::Url;
use puffin_package::pypi_types;
@ -9,6 +8,9 @@ pub enum Error {
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
#[error("{0} isn't available locally, but making network requests to registries was banned.")]
NoIndex(String),
/// The package was not found in the registry.
///
/// Make sure the package name is spelled correctly and that you've
@ -21,12 +23,8 @@ pub enum Error {
MetadataParseError(#[from] pypi_types::Error),
/// The metadata file was not found in the registry.
#[error("File `{0}` was not found in the registry.")]
FileNotFound(String),
/// The resource was not found in the registry.
#[error("Resource `{0}` was not found in the registry.")]
ResourceNotFound(Url),
#[error("File `{0}` was not found in the registry at {1}.")]
FileNotFound(String, #[source] reqwest_middleware::Error),
/// A generic request error happened while making a request. Refer to the
/// error message for more details.