Skip to content

Commit

Permalink
Don't display headers in spurious warning message.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ehuss committed Apr 3, 2023
1 parent 45e4b19 commit d74d0bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
36 changes: 25 additions & 11 deletions src/cargo/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use anyhow::Error;
use curl::easy::Easy;
use std::fmt;
use std::fmt::{self, Write};
use std::path::PathBuf;

use super::truncate_with_ellipsis;
Expand Down Expand Up @@ -50,27 +50,41 @@ impl HttpNotSuccessful {
headers,
}
}
}

impl fmt::Display for HttpNotSuccessful {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// Renders the error in a compact form.
pub fn display_short(&self) -> String {
self.render(false)
}

fn render(&self, show_headers: bool) -> String {
let mut result = String::new();
let body = std::str::from_utf8(&self.body)
.map(|s| truncate_with_ellipsis(s, 512))
.unwrap_or_else(|_| format!("[{} non-utf8 bytes]", self.body.len()));

write!(
f,
result,
"failed to get successful HTTP response from `{}`",
self.url
)?;
)
.unwrap();
if let Some(ip) = &self.ip {
write!(f, " ({ip})")?;
write!(result, " ({ip})").unwrap();
}
write!(f, ", got {}\n", self.code,)?;
if !self.headers.is_empty() {
write!(f, "debug headers:\n{}\n", self.headers.join("\n"))?;
write!(result, ", got {}\n", self.code).unwrap();
if show_headers {
if !self.headers.is_empty() {
write!(result, "debug headers:\n{}\n", self.headers.join("\n")).unwrap();
}
}
write!(f, "body:\n{body}",)
write!(result, "body:\n{body}").unwrap();
result
}
}

impl fmt::Display for HttpNotSuccessful {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.render(true))
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/cargo/util/network/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ impl<'a> Retry<'a> {
pub fn r#try<T>(&mut self, f: impl FnOnce() -> CargoResult<T>) -> RetryResult<T> {
match f() {
Err(ref e) if maybe_spurious(e) && self.retries < self.max_retries => {
let err_msg = e
.downcast_ref::<HttpNotSuccessful>()
.map(|http_err| http_err.display_short())
.unwrap_or_else(|| e.root_cause().to_string());
let msg = format!(
"spurious network error ({} tries remaining): {}",
"spurious network error ({} tries remaining): {err_msg}",
self.max_retries - self.retries,
e.root_cause(),
);
if let Err(e) = self.config.shell().warn(msg) {
return RetryResult::Err(e);
Expand Down
24 changes: 0 additions & 24 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3179,26 +3179,14 @@ fn debug_header_message_index() {
[UPDATING] `dummy-registry` index
warning: spurious network error (3 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
warning: spurious network error (2 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
warning: spurious network error (1 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/index/3/b/bar` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
error: failed to get `bar` as a dependency of package `foo v0.1.0 ([ROOT]/foo)`
Expand Down Expand Up @@ -3252,26 +3240,14 @@ fn debug_header_message_dl() {
[DOWNLOADING] crates ...
warning: spurious network error (3 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
warning: spurious network error (2 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
warning: spurious network error (1 tries remaining): \
failed to get successful HTTP response from `http://127.0.0.1:[..]/dl/bar/1.0.0/download` (127.0.0.1), got 503
debug headers:
x-amz-cf-pop: SFO53-P2
x-amz-cf-id: vEc3osJrCAXVaciNnF4Vev-hZFgnYwmNZtxMKRJ5bF6h9FTOtbTMnA==
x-cache: Hit from cloudfront
body:
Please slow down
error: failed to download from `http://127.0.0.1:[..]/dl/bar/1.0.0/download`
Expand Down

0 comments on commit d74d0bd

Please sign in to comment.