Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/uv-client/src/cached_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ impl CachedClient {
.execute(req)
.instrument(info_span!("revalidation_request", url = url.as_str()))
.await
.map_err(|err| ErrorKind::from_reqwest_middleware(url.clone(), err))?;
.map_err(|err| Error::from_reqwest_middleware(url.clone(), err))?;

// Check for HTTP error status and extract problem details if available
if let Err(status_error) = response.error_for_status_ref() {
Expand Down Expand Up @@ -610,7 +610,7 @@ impl CachedClient {
.0
.execute(req)
.await
.map_err(|err| ErrorKind::from_reqwest_middleware(url.clone(), err))?;
.map_err(|err| Error::from_reqwest_middleware(url.clone(), err))?;

// If the user set a custom `Cache-Control` header, override it.
if let CacheControl::Override(header) = cache_control {
Expand Down
67 changes: 28 additions & 39 deletions crates/uv-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ impl Error {
ErrorKind::BadMessagePack { source: err, url }.into()
}

/// Create an [`Error`] from a [`reqwest_middleware::Error`].
pub(crate) fn from_reqwest_middleware(
url: DisplaySafeUrl,
err: reqwest_middleware::Error,
) -> Self {
if let reqwest_middleware::Error::Middleware(ref underlying) = err {
if let Some(offline_err) = underlying.downcast_ref::<OfflineError>() {
return ErrorKind::Offline(offline_err.url().to_string()).into();
}
if let Some(reqwest_retry::RetryError::WithRetries { retries, .. }) =
underlying.downcast_ref::<reqwest_retry::RetryError>()
{
let retries = *retries;
return Self::new(
ErrorKind::WrappedReqwestError(url, WrappedReqwestError::from(err)),
retries,
);
}
}
Self::from(ErrorKind::WrappedReqwestError(
url,
WrappedReqwestError::from(err),
))
}

/// Returns `true` if this error corresponds to an offline error.
pub(crate) fn is_offline(&self) -> bool {
matches!(&*self.kind, ErrorKind::Offline(_))
Expand Down Expand Up @@ -246,15 +271,9 @@ impl Error {

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
match kind {
ErrorKind::RequestWithRetries { source, retries } => Self {
kind: source,
retries,
},
other => Self {
kind: Box::new(other),
retries: 0,
},
Self {
kind: Box::new(kind),
retries: 0,
}
}
}
Expand Down Expand Up @@ -306,10 +325,6 @@ pub enum ErrorKind {
#[error("Failed to fetch: `{0}`")]
WrappedReqwestError(DisplaySafeUrl, #[source] WrappedReqwestError),

/// Add the number of failed retries to the error.
#[error("Request failed after {retries} {subject}", subject = if *retries > 1 { "retries" } else { "retry" })]
RequestWithRetries { source: Box<Self>, retries: u32 },

#[error("Received some unexpected JSON from {}", url)]
BadJson {
source: serde_json::Error,
Expand Down Expand Up @@ -388,32 +403,6 @@ impl ErrorKind {
Self::WrappedReqwestError(url, WrappedReqwestError::from(error))
}

/// Create an [`ErrorKind`] from a [`reqwest_middleware::Error`].
pub(crate) fn from_reqwest_middleware(
url: DisplaySafeUrl,
err: reqwest_middleware::Error,
) -> Self {
if let reqwest_middleware::Error::Middleware(ref underlying) = err {
if let Some(err) = underlying.downcast_ref::<OfflineError>() {
return Self::Offline(err.url().to_string());
}
if let Some(reqwest_retry::RetryError::WithRetries { retries, .. }) =
underlying.downcast_ref::<reqwest_retry::RetryError>()
{
let retries = *retries;
return Self::RequestWithRetries {
source: Box::new(Self::WrappedReqwestError(
url,
WrappedReqwestError::from(err),
)),
retries,
};
}
}

Self::WrappedReqwestError(url, WrappedReqwestError::from(err))
}

/// Create an [`ErrorKind`] from a [`reqwest::Error`] with problem details.
pub(crate) fn from_reqwest_with_problem_details(
url: DisplaySafeUrl,
Expand Down
Loading