Skip to content

Commit 1d5677b

Browse files
author
Eric Swanson
authored
fix: display reqwest::Error source (#3832)
Since reqwest 0.12.1 (seanmonstar/reqwest#2199), fmt::Display for reqwest::Error no longer includes the source. The whole reason for WrappedReqwestError was to avoid displaying the source twice. But since the above change, the source isn't displayed at all. After this change, dfx will once again display the error source(s) for request::Error, but on separate lines ("Caused by:") like other error sources.
1 parent 349d547 commit 1d5677b

File tree

8 files changed

+28
-48
lines changed

8 files changed

+28
-48
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# UNRELEASED
44

5+
# fix: display error cause of some http-related errors
6+
7+
Some commands that download http resources, for example `dfx extension install`, will
8+
once again display any error cause.
9+
510
# 0.22.0
611

712
### asset uploads: retry some HTTP errors returned by the replica

src/dfx-core/src/error/extension.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(dead_code)]
2-
use crate::error::reqwest::WrappedReqwestError;
32
use crate::error::structured_file::StructuredFileError;
43
use thiserror::Error;
54

@@ -84,7 +83,7 @@ pub enum NewExtensionManagerError {
8483
#[derive(Error, Debug)]
8584
pub enum DownloadAndInstallExtensionToTempdirError {
8685
#[error(transparent)]
87-
ExtensionDownloadFailed(WrappedReqwestError),
86+
ExtensionDownloadFailed(reqwest::Error),
8887

8988
#[error("Cannot get extensions directory")]
9089
EnsureExtensionDirExistsFailed(#[source] crate::error::fs::FsError),
@@ -141,10 +140,10 @@ pub enum GetDependenciesError {
141140
ParseUrl(#[from] url::ParseError),
142141

143142
#[error(transparent)]
144-
Get(WrappedReqwestError),
143+
Get(reqwest::Error),
145144

146145
#[error(transparent)]
147-
ParseJson(WrappedReqwestError),
146+
ParseJson(reqwest::Error),
148147
}
149148

150149
#[derive(Error, Debug)]

src/dfx-core/src/error/reqwest.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
use crate::http::retryable::Retryable;
21
use reqwest::StatusCode;
3-
use thiserror::Error;
42

5-
// reqwest::Error's fmt::Display appends the error descriptions of all sources.
6-
// For this reason, it is not marked as #[source] here, so that we don't
7-
// display the error descriptions of all sources repeatedly.
8-
#[derive(Error, Debug)]
9-
#[error("{}", .0)]
10-
pub struct WrappedReqwestError(pub reqwest::Error);
11-
12-
impl Retryable for WrappedReqwestError {
13-
fn is_retryable(&self) -> bool {
14-
let err = &self.0;
15-
err.is_timeout()
16-
|| err.is_connect()
17-
|| matches!(
18-
err.status(),
19-
Some(
20-
StatusCode::INTERNAL_SERVER_ERROR
21-
| StatusCode::BAD_GATEWAY
22-
| StatusCode::SERVICE_UNAVAILABLE
23-
| StatusCode::GATEWAY_TIMEOUT
24-
| StatusCode::TOO_MANY_REQUESTS
25-
)
3+
pub fn is_retryable(err: &reqwest::Error) -> bool {
4+
err.is_timeout()
5+
|| err.is_connect()
6+
|| matches!(
7+
err.status(),
8+
Some(
9+
StatusCode::INTERNAL_SERVER_ERROR
10+
| StatusCode::BAD_GATEWAY
11+
| StatusCode::SERVICE_UNAVAILABLE
12+
| StatusCode::GATEWAY_TIMEOUT
13+
| StatusCode::TOO_MANY_REQUESTS
2614
)
27-
}
15+
)
2816
}

src/dfx-core/src/extension/manager/install.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::error::extension::{
33
GetExtensionArchiveNameError, GetExtensionDownloadUrlError, GetHighestCompatibleVersionError,
44
InstallExtensionError,
55
};
6-
use crate::error::reqwest::WrappedReqwestError;
76
use crate::extension::{
87
manager::ExtensionManager, manifest::ExtensionDependencies, url::ExtensionJsonUrl,
98
};
@@ -96,11 +95,10 @@ impl ExtensionManager {
9695
.await
9796
.map_err(DownloadAndInstallExtensionToTempdirError::ExtensionDownloadFailed)?;
9897

99-
let bytes = response.bytes().await.map_err(|e| {
100-
DownloadAndInstallExtensionToTempdirError::ExtensionDownloadFailed(WrappedReqwestError(
101-
e,
102-
))
103-
})?;
98+
let bytes = response
99+
.bytes()
100+
.await
101+
.map_err(DownloadAndInstallExtensionToTempdirError::ExtensionDownloadFailed)?;
104102

105103
crate::fs::composite::ensure_dir_exists(&self.dir)
106104
.map_err(DownloadAndInstallExtensionToTempdirError::EnsureExtensionDirExistsFailed)?;

src/dfx-core/src/extension/manifest/dependencies.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::error::extension::{DfxOnlySupportedDependency, GetDependenciesError};
2-
use crate::error::reqwest::WrappedReqwestError;
32
use crate::extension::url::ExtensionJsonUrl;
43
use crate::http::get::get_with_retries;
54
use crate::json::structure::VersionReqWithJsonSchema;
@@ -36,9 +35,7 @@ impl ExtensionDependencies {
3635
.await
3736
.map_err(GetDependenciesError::Get)?;
3837

39-
resp.json()
40-
.await
41-
.map_err(|e| GetDependenciesError::ParseJson(WrappedReqwestError(e)))
38+
resp.json().await.map_err(GetDependenciesError::ParseJson)
4239
}
4340

4441
pub fn find_highest_compatible_version(

src/dfx-core/src/http/get.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::error::reqwest::WrappedReqwestError;
2-
use crate::http::retryable::Retryable;
31
use backoff::exponential::ExponentialBackoff;
42
use backoff::future::retry;
53
use backoff::SystemClock;
@@ -9,15 +7,14 @@ use url::Url;
97
pub async fn get_with_retries(
108
url: Url,
119
retry_policy: ExponentialBackoff<SystemClock>,
12-
) -> Result<Response, WrappedReqwestError> {
10+
) -> Result<Response, reqwest::Error> {
1311
let operation = || async {
1412
let response = reqwest::get(url.clone())
1513
.await
16-
.and_then(|resp| resp.error_for_status())
17-
.map_err(WrappedReqwestError);
14+
.and_then(|resp| resp.error_for_status());
1815
match response {
1916
Ok(doc) => Ok(doc),
20-
Err(e) if e.is_retryable() => Err(backoff::Error::transient(e)),
17+
Err(e) if crate::error::reqwest::is_retryable(&e) => Err(backoff::Error::transient(e)),
2118
Err(e) => Err(backoff::Error::permanent(e)),
2219
}
2320
};

src/dfx-core/src/http/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
pub mod get;
2-
pub mod retryable;

src/dfx-core/src/http/retryable.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)