diff options
author | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-01 12:36:24 +0530 |
---|---|---|
committer | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-01 12:36:24 +0530 |
commit | c1cbcf355755fbd1ca6124cdba3b8e361a7bebf2 (patch) | |
tree | 4f2120ad0e7546423bec08711df84984fc0bfd6c /src | |
parent | c50332cead2688e40de192e1b47e50a662763a78 (diff) |
Better error reporting
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 4 |
2 files changed, 19 insertions, 10 deletions
@@ -39,12 +39,12 @@ mod common { #[derive(Default)] pub struct WalkResult { - pub num_errors: usize, + pub num_errors: u64, } impl fmt::Display for WalkResult { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "Encountered {} IO errors", self.num_errors) + write!(f, "Encountered {} IO error(s)", self.num_errors) } } } @@ -52,6 +52,7 @@ mod common { mod aggregate { use crate::{WalkOptions, WalkResult}; use failure::Error; + use std::borrow::Cow; use std::{io, path::Path}; pub fn aggregate( @@ -65,6 +66,7 @@ mod aggregate { for path in paths.into_iter() { num_roots += 1; let mut num_bytes = 0u64; + let mut num_errors = 0u64; for entry in options.iter_from_path(path.as_ref()) { match entry { Ok(entry) => { @@ -72,7 +74,7 @@ mod aggregate { Some(Ok(ref m)) if !m.is_dir() => m.len(), Some(Ok(_)) => 0, Some(Err(_)) => { - res.num_errors += 1; + num_errors += 1; 0 } None => unreachable!( @@ -80,15 +82,16 @@ mod aggregate { ), }; } - Err(_) => res.num_errors += 1, + Err(_) => num_errors += 1, } } - write_path(&mut out, &options, path, num_bytes)?; + write_path(&mut out, &options, path, num_bytes, num_errors)?; total += num_bytes; + res.num_errors += num_errors; } if num_roots > 1 { - write_path(&mut out, &options, Path::new("<TOTAL>"), total)?; + write_path(&mut out, &options, Path::new("total"), total, res.num_errors)?; } Ok(res) } @@ -98,12 +101,18 @@ mod aggregate { options: &WalkOptions, path: impl AsRef<Path>, num_bytes: u64, + num_errors: u64, ) -> Result<(), io::Error> { writeln!( out, - "{}\t{}", + "{}\t{}{}", options.format_bytes(num_bytes), - path.as_ref().display() + path.as_ref().display(), + if num_errors == 0 { + Cow::Borrowed("") + } else { + Cow::Owned(format!("\t<{} IO Error(s)>", num_errors)) + } ) } } diff --git a/src/main.rs b/src/main.rs index 452d739..32154c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use structopt::StructOpt; use dua::ByteFormat; use failure::Error; use failure_tools::ok_or_exit; -use std::{io, path::PathBuf}; +use std::{io, path::PathBuf, process}; mod options { use dua::ByteFormat as LibraryByteFormat; @@ -93,7 +93,7 @@ fn run() -> Result<(), Error> { }?; if res.num_errors > 0 { - writeln!(io::stderr(), "{}", res).ok(); + process::exit(1); } Ok(()) } |