Make external app error uniform (#812)

This commit is contained in:
JT 2022-01-22 09:12:34 -05:00 committed by GitHub
parent 47d004ae24
commit b58aad5eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 9 deletions

View File

@ -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(()),
}
});

View File

@ -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))]

View File

@ -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"
}