140140//! [Cargo Contributor Guide]: https://doc.crates.io/contrib/
141141
142142use crate :: core:: Shell ;
143+ use crate :: core:: shell:: Verbosity ;
143144use crate :: core:: shell:: Verbosity :: Verbose ;
144145use anyhow:: Error ;
145146use 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