summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Peter <mail@david-peter.de>2021-08-08 13:02:52 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2021-08-08 13:10:16 +0200
commitb507449146be2fb113ab117f05ed46ccd2fcb94f (patch)
tree0afe010f2c81c7db1a09af4577118f507c389c99
parentd9697d14867c59dad487bb202c23840a430a6bb0 (diff)
Handle IO errors based on type, closes #737
-rw-r--r--src/output.rs12
-rw-r--r--tests/tests.rs2
2 files changed, 10 insertions, 4 deletions
diff --git a/src/output.rs b/src/output.rs
index 5705087..471b8fb 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -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;
@@ -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());
+ }
}
}
diff --git a/tests/tests.rs b/tests/tests.rs
index 056a064..783420a 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -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