Skip to content

Commit d74d0bd

Browse files
committed
Don't display headers in spurious warning message.
The headers can significantly contribute to noise in the output, drowning out the rest of the output. Most investigation will likely be focused on the case where cargo completely fails to download, so this only shows the full detail in the final error message.
1 parent 45e4b19 commit d74d0bd

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

src/cargo/util/errors.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use anyhow::Error;
44
use curl::easy::Easy;
5-
use std::fmt;
5+
use std::fmt::{self, Write};
66
use std::path::PathBuf;
77

88
use super::truncate_with_ellipsis;
@@ -50,27 +50,41 @@ impl HttpNotSuccessful {
5050
headers,
5151
}
5252
}
53-
}
5453

55-
impl fmt::Display for HttpNotSuccessful {
56-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
/// Renders the error in a compact form.
55+
pub fn display_short(&self) -> String {
56+
self.render(false)
57+
}
58+
59+
fn render(&self, show_headers: bool) -> String {
60+
let mut result = String::new();
5761
let body = std::str::from_utf8(&self.body)
5862
.map(|s| truncate_with_ellipsis(s, 512))
5963
.unwrap_or_else(|_| format!("[{} non-utf8 bytes]", self.body.len()));
6064

6165
write!(
62-
f,
66+
result,
6367
"failed to get successful HTTP response from `{}`",
6468
self.url
65-
)?;
69+
)
70+
.unwrap();
6671
if let Some(ip) = &self.ip {
67-
write!(f, " ({ip})")?;
72+
write!(result, " ({ip})").unwrap();
6873
}
69-
write!(f, ", got {}\n", self.code,)?;
70-
if !self.headers.is_empty() {
71-
write!(f, "debug headers:\n{}\n", self.headers.join("\n"))?;
74+
write!(result, ", got {}\n", self.code).unwrap();
75+
if show_headers {
76+
if !self.headers.is_empty() {
77+
write!(result, "debug headers:\n{}\n", self.headers.join("\n")).unwrap();
78+
}
7279
}
73-
write!(f, "body:\n{body}",)
80+
write!(result, "body:\n{body}").unwrap();
81+
result
82+
}
83+
}
84+
85+
impl fmt::Display for HttpNotSuccessful {
86+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87+
f.write_str(&self.render(true))
7488
}
7589
}
7690

src/cargo/util/network/retry.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ impl<'a> Retry<'a> {
4444
pub fn r#try<T>(&mut self, f: impl FnOnce() -> CargoResult<T>) -> RetryResult<T> {
4545
match f() {
4646
Err(ref e) if maybe_spurious(e) && self.retries < self.max_retries => {
47+
let err_msg = e
48+
.downcast_ref::<HttpNotSuccessful>()
49+
.map(|http_err| http_err.display_short())
50+
.unwrap_or_else(|| e.root_cause().to_string());
4751
let msg = format!(
48-
"spurious network error ({} tries remaining): {}",
52+
"spurious network error ({} tries remaining): {err_msg}",
4953
self.max_retries - self.retries,
50-
e.root_cause(),
5154
);
5255
if let Err(e) = self.config.shell().warn(msg) {
5356
return RetryResult::Err(e);

tests/testsuite/registry.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,26 +3179,14 @@ fn debug_header_message_index() {
31793179
[UPDATING] `dummy-registry` index
31803180
warning: spurious network error (3 tries remaining): \
31813181
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3182-
debug headers:
3183-
x-amz-cf-pop: SFO53-P2
3184-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3185-
x-cache: Hit from cloudfront
31863182
body:
31873183
Please slow down
31883184
warning: spurious network error (2 tries remaining): \
31893185
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3190-
debug headers:
3191-
x-amz-cf-pop: SFO53-P2
3192-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3193-
x-cache: Hit from cloudfront
31943186
body:
31953187
Please slow down
31963188
warning: spurious network error (1 tries remaining): \
31973189
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3198-
debug headers:
3199-
x-amz-cf-pop: SFO53-P2
3200-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3201-
x-cache: Hit from cloudfront
32023190
body:
32033191
Please slow down
32043192
error: failed to get `bar` as a dependency of package `foo v0.1.0 ([ROOT]/foo)`
@@ -3252,26 +3240,14 @@ fn debug_header_message_dl() {
32523240
[DOWNLOADING] crates ...
32533241
warning: spurious network error (3 tries remaining): \
32543242
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3255-
debug headers:
3256-
x-amz-cf-pop: SFO53-P2
3257-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3258-
x-cache: Hit from cloudfront
32593243
body:
32603244
Please slow down
32613245
warning: spurious network error (2 tries remaining): \
32623246
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3263-
debug headers:
3264-
x-amz-cf-pop: SFO53-P2
3265-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3266-
x-cache: Hit from cloudfront
32673247
body:
32683248
Please slow down
32693249
warning: spurious network error (1 tries remaining): \
32703250
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3271-
debug headers:
3272-
x-amz-cf-pop: SFO53-P2
3273-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3274-
x-cache: Hit from cloudfront
32753251
body:
32763252
Please slow down
32773253
error: failed to download from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`

0 commit comments

Comments
 (0)