8000 refactor(cli): Pull out error chain iteration by epage · Pull Request #15926 · rust-lang/cargo · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
29 changes: 16 additions & 13 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
//! [Cargo Contributor Guide]: https://doc.crates.io/contrib/

use crate::core::Shell;
use crate::core::shell::Verbosity;
use crate::core::shell::Verbosity::Verbose;
use anyhow::Error;
use tracing::debug;
Expand Down Expand Up @@ -206,18 +207,21 @@ pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell)
_display_error(err, shell, false);
}

fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
for (i, err) in err.chain().enumerate() {
// If we're not in verbose mode then only print cause chain until one
// marked as `VerboseError` appears.
//
// Generally the top error shouldn't be verbose, but check it anyways.
if shell.verbosity() != Verbose && err.is::<VerboseError>() {
return true;
}
if err.is::<AlreadyPrintedError>() {
break;
}
fn error_chain(err: &Error, verbosity: Verbosity) -> impl Iterator<Item = &dyn std::fmt::Display> {
err.chain()
.take_while(move |err| {
// If we're not in verbose mode then only print cause chain until one
// marked as `VerboseError` appears.
//
// Generally the top error shouldn't be verbose, but check it anyways.
verbosity == Verbose || !err.is::<VerboseError>()
})
.take_while(|err| !err.is::<AlreadyPrintedError>())
.map(|err| err as &dyn std::fmt::Display)
}

fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) {
for (i, err) in error_chain(err, shell.verbosity()).enumerate() {
if i == 0 {
if as_err {
drop(shell.error(&err));
Expand All @@ -229,5 +233,4 @@ fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
drop(write!(shell.err(), "{}", indented_lines(&err.to_string())));
}
}
false
}
Loading
0