Skip to content

Commit

Permalink
Handle IO errors based on type, closes #737
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Aug 8, 2021
1 parent d9697d1 commit b507449
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;

use lscolors::{LsColors, Style};

use crate::error::print_error;
use crate::exit_codes::ExitCode;
use crate::filesystem::strip_current_dir;
use crate::options::Options;
Expand Down Expand Up @@ -33,9 +34,14 @@ pub fn print_entry(
print_entry_uncolorized(stdout, path, config)
};

if r.is_err() {
// Probably a broken pipe. Exit gracefully.
process::exit(ExitCode::GeneralError.into());
if let Err(e) = r {
if e.kind() == ::std::io::ErrorKind::BrokenPipe {
// Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').
process::exit(0);
} else {
print_error(format!("Could not write to output: {}", e));
process::exit(ExitCode::GeneralError.into());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ fn test_base_directory() {
let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);
let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);
te.assert_output(
&["--base-directory", &abs_base_dir, "foo", &abs_path],
&["--base-directory", abs_base_dir, "foo", &abs_path],
&format!(
"{abs_path}/a.foo
{abs_path}/one/b.foo
Expand Down

0 comments on commit b507449

Please sign in to comment.