Skip to content

Commit c00a633

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 4702fa3 commit c00a633

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
@@ -3319,26 +3319,14 @@ fn debug_header_message_index() {
33193319
[UPDATING] `dummy-registry` index
33203320
warning: spurious network error (3 tries remaining): \
33213321
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3322-
debug headers:
3323-
x-amz-cf-pop: SFO53-P2
3324-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3325-
x-cache: Hit from cloudfront
33263322
body:
33273323
Please slow down
33283324
warning: spurious network error (2 tries remaining): \
33293325
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3330-
debug headers:
3331-
x-amz-cf-pop: SFO53-P2
3332-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3333-
x-cache: Hit from cloudfront
33343326
body:
33353327
Please slow down
33363328
warning: spurious network error (1 tries remaining): \
33373329
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
3338-
debug headers:
3339-
x-amz-cf-pop: SFO53-P2
3340-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3341-
x-cache: Hit from cloudfront
33423330
body:
33433331
Please slow down
33443332
error: failed to get `bar` as a dependency of package `foo v0.1.0 ([ROOT]/foo)`
@@ -3393,26 +3381,14 @@ fn debug_header_message_dl() {
33933381
[DOWNLOADING] crates ...
33943382
warning: spurious network error (3 tries remaining): \
33953383
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3396-
debug headers:
3397-
x-amz-cf-pop: SFO53-P2
3398-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3399-
x-cache: Hit from cloudfront
34003384
body:
34013385
Please slow down
34023386
warning: spurious network error (2 tries remaining): \
34033387
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3404-
debug headers:
3405-
x-amz-cf-pop: SFO53-P2
3406-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3407-
x-cache: Hit from cloudfront
34083388
body:
34093389
Please slow down
34103390
warning: spurious network error (1 tries remaining): \
34113391
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
3412-
debug headers:
3413-
x-amz-cf-pop: SFO53-P2
3414-
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
3415-
x-cache: Hit from cloudfront
34163392
body:
34173393
Please slow down
34183394
error: failed to download from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`

0 commit comments

Comments
 (0)