From b58aad5eb0020c7ccd8caf69e9047727b78e45f3 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 22 Jan 2022 09:12:34 -0500 Subject: [PATCH] Make external app error uniform (#812) --- crates/nu-command/src/system/run_external.rs | 14 ++++++++++++-- crates/nu-protocol/src/shell_error.rs | 4 ++-- src/tests.rs | 6 +----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index c22f1957f..6103af4a1 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -67,6 +67,8 @@ impl Command for External { } else { return Err(ShellError::ExternalCommand( "Cannot convert argument to a string".into(), + "All arguments to an external command need to be string-compatible" + .into(), val.span()?, )); } @@ -74,6 +76,7 @@ impl Command for External { } else { return Err(ShellError::ExternalCommand( "Cannot convert argument to a string".into(), + "All arguments to an external command need to be string-compatible".into(), arg.span()?, )); } @@ -141,7 +144,8 @@ impl<'call> ExternalCommand<'call> { match process.spawn() { Err(err) => Err(ShellError::ExternalCommand( - format!("{}", err), + "can't run executable".to_string(), + err.to_string(), self.name.span, )), Ok(mut child) => { @@ -186,6 +190,8 @@ impl<'call> ExternalCommand<'call> { let stdout = child.stdout.take().ok_or_else(|| { ShellError::ExternalCommand( "Error taking stdout from external".to_string(), + "Redirects need access to stdout of an external command" + .to_string(), span, ) })?; @@ -220,7 +226,11 @@ impl<'call> ExternalCommand<'call> { } match child.wait() { - Err(err) => Err(ShellError::ExternalCommand(format!("{}", err), span)), + Err(err) => Err(ShellError::ExternalCommand( + "External command exited with error".into(), + err.to_string(), + span, + )), Ok(_) => Ok(()), } }); diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index be7876a39..af163fa0a 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -142,8 +142,8 @@ pub enum ShellError { ), #[error("External command")] - #[diagnostic(code(nu::shell::external_command), url(docsrs))] - ExternalCommand(String, #[label("{0}")] Span), + #[diagnostic(code(nu::shell::external_command), url(docsrs), help("{1}"))] + ExternalCommand(String, String, #[label("{0}")] Span), #[error("Unsupported input")] #[diagnostic(code(nu::shell::unsupported_input), url(docsrs))] diff --git a/src/tests.rs b/src/tests.rs index e767ce58a..a34d6a477 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -75,9 +75,5 @@ pub fn fail_test(input: &str, expected: &str) -> TestResult { #[cfg(test)] pub fn not_found_msg() -> &'static str { - if cfg!(windows) { - "not found" - } else { - "No such" - } + "can't run executable" }