on master help sign in
Fix process spawning error handling (#3349)

* Fix process spawning error handling * Log stderr in any case

commitd2cec25395e01ded0977c1314e377cb24186c6c5 authorPiergiorgioZagaria <75474806+PiergiorgioZagaria@users.noreply.github.com> dateTue 30 Aug 2022, 04:02 (4 years ago) parent5f043dd
2 files changed+30−14
helix-term/src/commands.rs+12−2 helix-view/src/document.rs+18−12
helix-term/src/commands.rs+12−2
@@ -4596,8 +4596,18 @@ fn shell_impl(
45964596 }
45974597 let output = process.wait_with_output()?;
45984598
4599− if !output.stderr.is_empty() {
4600− log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
4599+ if !output.status.success() {
4600+ if !output.stderr.is_empty() {
4601+ let err = String::from_utf8_lossy(&output.stderr).to_string();
4602+ log::error!("Shell error: {}", err);
4603+ bail!("Shell error: {}", err);
4604+ }
4605+ bail!("Shell command failed");
4606+ } else if !output.stderr.is_empty() {
4607+ log::debug!(
4608+ "Command printed to stderr: {}",
4609+ String::from_utf8_lossy(&output.stderr).to_string()
4610+ );
46014611 }
46024612
46034613 let str = std::str::from_utf8(&output.stdout)
helix-view/src/document.rs+18−12
@@ -440,17 +440,22 @@ impl Document {
440440 .await
441441 .map_err(|_| FormatterError::WaitForOutputFailed)?;
442442
443− if !output.stderr.is_empty() {
444− return Err(FormatterError::Stderr(
445− String::from_utf8_lossy(&output.stderr).to_string(),
446− ));
447− }
448−
449443 if !output.status.success() {
450− return Err(FormatterError::NonZeroExitStatus);
444+ if !output.stderr.is_empty() {
445+ let err = String::from_utf8_lossy(&output.stderr).to_string();
446+ log::error!("Formatter error: {}", err);
447+ return Err(FormatterError::NonZeroExitStatus(Some(err)));
448+ }
449+
450+ return Err(FormatterError::NonZeroExitStatus(None));
451+ } else if !output.stderr.is_empty() {
452+ log::debug!(
453+ "Formatter printed to stderr: {}",
454+ String::from_utf8_lossy(&output.stderr).to_string()
455+ );
451456 }
452457
453− let str = String::from_utf8(output.stdout)
458+ let str = std::str::from_utf8(&output.stdout)
454459 .map_err(|_| FormatterError::InvalidUtf8Output)?;
455460
456461 Ok(helix_core::diff::compare_ropes(&text, &Rope::from(str)))
@@ -1102,10 +1107,9 @@ pub enum FormatterError {
11021107 },
11031108 BrokenStdin,
11041109 WaitForOutputFailed,
1105− Stderr(String),
11061110 InvalidUtf8Output,
11071111 DiskReloadError(String),
1108− NonZeroExitStatus,
1112+ NonZeroExitStatus(Option<String>),
11091113}
11101114
11111115impl std::error::Error for FormatterError {}
@@ -1118,10 +1122,12 @@ impl Display for FormatterError {
11181122 }
11191123 Self::BrokenStdin => write!(f, "Could not write to formatter stdin"),
11201124 Self::WaitForOutputFailed => write!(f, "Waiting for formatter output failed"),
1121− Self::Stderr(output) => write!(f, "Formatter error: {}", output),
11221125 Self::InvalidUtf8Output => write!(f, "Invalid UTF-8 formatter output"),
11231126 Self::DiskReloadError(error) => write!(f, "Error reloading file from disk: {}", error),
1124− Self::NonZeroExitStatus => write!(f, "Formatter exited with non zero exit status:"),
1127+ Self::NonZeroExitStatus(Some(output)) => write!(f, "Formatter error: {}", output),
1128+ Self::NonZeroExitStatus(None) => {
1129+ write!(f, "Formatter exited with non zero exit status")
1130+ }
11251131 }
11261132 }
11271133}