From 5342cedf4a3fe48e8d480a675b4f431430635bae Mon Sep 17 00:00:00 2001 From: Aminu 'Seun Joshua Date: Fri, 11 Jul 2025 01:01:24 +0100 Subject: [PATCH] - isolate and handle unix error Signed-off-by: Aminu 'Seun Joshua - refactor: separate error code Signed-off-by: Aminu 'Seun Joshua - wasm target included + update to error convention Signed-off-by: Aminu 'Seun Joshua - fix: build workflow Signed-off-by: Aminu 'Seun Joshua - fix: template issue with zig + docs Signed-off-by: Aminu 'Seun Joshua - revert zig template Signed-off-by: Aminu 'Seun Joshua - specify zig version for ci Signed-off-by: Aminu 'Seun Joshua - revert workflow + re-word response Signed-off-by: Aminu 'Seun Joshua - revert version edit Signed-off-by: Aminu 'Seun Joshua - revert build workflow Signed-off-by: Aminu 'Seun Joshua --- src/subprocess.rs | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/subprocess.rs b/src/subprocess.rs index ef74f68f01..81d384ea8b 100644 --- a/src/subprocess.rs +++ b/src/subprocess.rs @@ -4,19 +4,45 @@ /// When this error is encountered the cli will exit with the status code /// instead of printing an error, #[derive(Debug)] -pub struct ExitStatusError { - status: Option, +pub enum ExitStatusError { + /// The subprocess exited with a specific exit code. + ExitCode(i32), + /// The subprocess was exited by a signal. Only available for `Unix` based systems. + Signal(i32), + /// Catchall for general errors. Error code `1` + Unknown, } impl ExitStatusError { + #[cfg(unix)] pub(crate) fn new(status: std::process::ExitStatus) -> Self { - Self { - status: status.code(), + use std::os::unix::process::ExitStatusExt as _; + + if let Some(code) = status.code() { + Self::ExitCode(code) + } else if let Some(signal) = status.signal() { + Self::Signal(signal) + } else { + Self::Unknown + } + } + + #[cfg(not(unix))] + pub(crate) fn new(status: std::process::ExitStatus) -> Self { + if let Some(code) = status.code() { + Self::ExitCode(code) + } else { + Self::Unknown } } pub fn code(&self) -> i32 { - self.status.unwrap_or(1) + match self { + Self::ExitCode(code) => *code, + Self::Signal(signal) => 128 + *signal, + Self::Unknown => 1, + } + .min(255) } } @@ -24,11 +50,12 @@ impl std::error::Error for ExitStatusError {} impl std::fmt::Display for ExitStatusError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let _ = write!(f, "subprocess exited with status: "); - if let Some(status) = self.status { - writeln!(f, "{status}") - } else { - writeln!(f, "unknown") + let _ = write!(f, "subprocess exited with "); + + match self { + Self::ExitCode(code) => write!(f, "exit code: {code}"), + Self::Signal(signal) => write!(f, "signal: {signal}"), + Self::Unknown => write!(f, "unknown status"), } } }