Skip to content

Commit

Permalink
Better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 1, 2019
1 parent c50332c commit c1cbcf3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ 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)
}
}
}

mod aggregate {
use crate::{WalkOptions, WalkResult};
use failure::Error;
use std::borrow::Cow;
use std::{io, path::Path};

pub fn aggregate(
Expand All @@ -65,30 +66,32 @@ 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) => {
num_bytes += match entry.metadata {
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!(
"we ask for metadata, so we at least have Some(Err(..))). Issue in jwalk?"
),
};
}
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)
}
Expand All @@ -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))
}
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +93,7 @@ fn run() -> Result<(), Error> {
}?;

if res.num_errors > 0 {
writeln!(io::stderr(), "{}", res).ok();
process::exit(1);
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.26 MB .
1.26 MB .
0.00 B foo <1 IO Error(s)>
0.00 B bar <1 IO Error(s)>
0.00 B baz <1 IO Error(s)>
2.52 MB total <3 IO Error(s)>
2 changes: 1 addition & 1 deletion tests/snapshots/success-no-arguments-multiple-input-paths
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
1.26 MB dir
1.26 MB ./dir/
256.00 KB ./dir/sub
5.29 MB <TOTAL>
5.29 MB total
7 changes: 7 additions & 0 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ snapshot="$root/snapshots"
fixtures="$root/fixtures"

SUCCESSFULLY=0
WITH_FAILURE=1

(with "a sample directory"
(sandbox
Expand All @@ -36,6 +37,12 @@ SUCCESSFULLY=0
expect_run ${SUCCESSFULLY} "$exe" . . dir ./dir/ ./dir/sub
}
)
(when "specifying no subcommand and some of the directories don't exist"
it "produces a human-readable (metric) aggregate of the current directory, with total" && {
WITH_SNAPSHOT="$snapshot/failure-no-arguments-multiple-input-paths-some-not-existing" \
expect_run ${WITH_FAILURE} "$exe" . . foo bar baz
}
)
)
)

Expand Down

0 comments on commit c1cbcf3

Please sign in to comment.