Reword shell errors

This commit is contained in:
Ian Manske 2024-08-01 20:57:59 -07:00
parent ee7478781e
commit 02dcd0036d
2 changed files with 27 additions and 20 deletions

View File

@ -1,6 +1,6 @@
use miette::Diagnostic; use miette::Diagnostic;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::io; use std::{io, num::NonZeroI32};
use thiserror::Error; use thiserror::Error;
use crate::{ use crate::{
@ -639,7 +639,7 @@ pub enum ShellError {
span: Span, span: Span,
}, },
/// An external command exited with a non-zero exit status. /// An external command exited with a non-zero exit code.
/// ///
/// ## Resolution /// ## Resolution
/// ///
@ -647,36 +647,38 @@ pub enum ShellError {
#[error("External command had a non-zero exit code")] #[error("External command had a non-zero exit code")]
#[diagnostic(code(nu::shell::non_zero_exit_code))] #[diagnostic(code(nu::shell::non_zero_exit_code))]
NonZeroExitCode { NonZeroExitCode {
exit_code: i32, exit_code: NonZeroI32,
#[label("exited with code {exit_code}")] #[label("exited with code {exit_code}")]
span: Span, span: Span,
}, },
/// A child process exited due to a signal. #[cfg(unix)]
/// An external command exited due to a signal.
/// ///
/// ## Resolution /// ## Resolution
/// ///
/// Check why the signal was sent or triggered. /// Check why the signal was sent or triggered.
#[error("Child process was terminated by a signal")] #[error("External command was terminated by a signal")]
#[diagnostic(code(nu::shell::process_terminated_by_signal))] #[diagnostic(code(nu::shell::terminated_by_signal))]
ProcessSignaled { TerminatedBySignal {
signal_name: String, signal_name: String,
signal: i32, signal: i32,
#[label("terminated due to {signal_name} {signal}")] #[label("terminated by {signal_name} ({signal})")]
span: Span, span: Span,
}, },
/// A child process core dumped. #[cfg(unix)]
/// An external command core dumped.
/// ///
/// ## Resolution /// ## Resolution
/// ///
/// Check why the core dumped was triggered. /// Check why the core dumped was triggered.
#[error("Child process core dumped")] #[error("External command core dumped")]
#[diagnostic(code(nu::shell::process_core_dumped))] #[diagnostic(code(nu::shell::core_dumped))]
ProcessCoreDumped { CoreDumped {
signal_name: String, signal_name: String,
signal: i32, signal: i32,
#[label("core dumped with {signal_name} {signal}")] #[label("core dumped with {signal_name} ({signal})")]
span: Span, span: Span,
}, },
@ -1424,9 +1426,9 @@ On Windows, this would be %USERPROFILE%\AppData\Roaming"#
impl ShellError { impl ShellError {
pub fn external_exit_code(&self) -> Option<Spanned<i32>> { pub fn external_exit_code(&self) -> Option<Spanned<i32>> {
let (item, span) = match *self { let (item, span) = match *self {
Self::NonZeroExitCode { exit_code, span } => (exit_code, span), Self::NonZeroExitCode { exit_code, span } => (exit_code.into(), span),
Self::ProcessSignaled { signal, span, .. } Self::TerminatedBySignal { signal, span, .. }
| Self::ProcessCoreDumped { signal, span, .. } => (-signal, span), | Self::CoreDumped { signal, span, .. } => (-signal, span),
_ => return None, _ => return None,
}; };
Some(Spanned { item, span }) Some(Spanned { item, span })

View File

@ -22,8 +22,13 @@ impl ExitStatus {
pub fn check_ok(self, span: Span) -> Result<(), ShellError> { pub fn check_ok(self, span: Span) -> Result<(), ShellError> {
match self { match self {
ExitStatus::Exited(0) => Ok(()), ExitStatus::Exited(exit_code) => {
ExitStatus::Exited(exit_code) => Err(ShellError::NonZeroExitCode { exit_code, span }), if let Ok(exit_code) = exit_code.try_into() {
Err(ShellError::NonZeroExitCode { exit_code, span })
} else {
Ok(())
}
}
#[cfg(unix)] #[cfg(unix)]
ExitStatus::Signaled { ExitStatus::Signaled {
signal, signal,
@ -39,13 +44,13 @@ impl ExitStatus {
} else { } else {
let signal_name = sig.map(Signal::as_str).unwrap_or("unknown signal").into(); let signal_name = sig.map(Signal::as_str).unwrap_or("unknown signal").into();
Err(if core_dumped { Err(if core_dumped {
ShellError::ProcessCoreDumped { ShellError::CoreDumped {
signal_name, signal_name,
signal, signal,
span, span,
} }
} else { } else {
ShellError::ProcessSignaled { ShellError::TerminatedBySignal {
signal_name, signal_name,
signal, signal,
span, span,