Skip to content

Commit a154b10

Browse files
authored
refactor(cli): Pull out error chain iteration (#15926)
### What does this PR try to resolve? This is some cleanup that will help with #15922 ### How to test and review this PR?
2 parents 761c465 + 4d341c9 commit a154b10

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/cargo/lib.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
//! [Cargo Contributor Guide]: https://doc.crates.io/contrib/
141141
142142
use crate::core::Shell;
143+
use crate::core::shell::Verbosity;
143144
use crate::core::shell::Verbosity::Verbose;
144145
use anyhow::Error;
145146
use tracing::debug;
@@ -206,18 +207,21 @@ pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell)
206207
_display_error(err, shell, false);
207208
}
208209

209-
fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
210-
for (i, err) in err.chain().enumerate() {
211-
// If we're not in verbose mode then only print cause chain until one
212-
// marked as `VerboseError` appears.
213-
//
214-
// Generally the top error shouldn't be verbose, but check it anyways.
215-
if shell.verbosity() != Verbose && err.is::<VerboseError>() {
216-
return true;
217-
}
218-
if err.is::<AlreadyPrintedError>() {
219-
break;
220-
}
210+
fn error_chain(err: &Error, verbosity: Verbosity) -> impl Iterator<Item = &dyn std::fmt::Display> {
211+
err.chain()
212+
.take_while(move |err| {
213+
// If we're not in verbose mode then only print cause chain until one
214+
// marked as `VerboseError` appears.
215+
//
216+
// Generally the top error shouldn't be verbose, but check it anyways.
217+
verbosity == Verbose || !err.is::<VerboseError>()
218+
})
219+
.take_while(|err| !err.is::<AlreadyPrintedError>())
220+
.map(|err| err as &dyn std::fmt::Display)
221+
}
222+
223+
fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) {
224+
for (i, err) in error_chain(err, shell.verbosity()).enumerate() {
221225
if i == 0 {
222226
if as_err {
223227
drop(shell.error(&err));
@@ -229,5 +233,4 @@ fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
229233
drop(write!(shell.err(), "{}", indented_lines(&err.to_string())));
230234
}
231235
}
232-
false
233236
}

0 commit comments

Comments
 (0)