From e3e6d1dc9c1ac51ae6273396be1503b70212b2b0 Mon Sep 17 00:00:00 2001 From: David Peter Date: Sun, 8 Aug 2021 13:02:52 +0200 Subject: [PATCH] Handle IO errors based on type, closes #737 --- src/output.rs | 12 +++++++++--- tests/tests.rs | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/output.rs b/src/output.rs index 57050878a..471b8fb9d 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 056a064ad..783420ad6 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