Skip to content

Commit 792b355

Browse files
authored
Merge pull request #3192 from seun-ja/improve-error-messages-when-trigger-subprocess-killed-by-signal
isolate and handle unix error
2 parents 7f2a6bf + 5342ced commit 792b355

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/subprocess.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,58 @@
44
/// When this error is encountered the cli will exit with the status code
55
/// instead of printing an error,
66
#[derive(Debug)]
7-
pub struct ExitStatusError {
8-
status: Option<i32>,
7+
pub enum ExitStatusError {
8+
/// The subprocess exited with a specific exit code.
9+
ExitCode(i32),
10+
/// The subprocess was exited by a signal. Only available for `Unix` based systems.
11+
Signal(i32),
12+
/// Catchall for general errors. Error code `1`
13+
Unknown,
914
}
1015

1116
impl ExitStatusError {
17+
#[cfg(unix)]
1218
pub(crate) fn new(status: std::process::ExitStatus) -> Self {
13-
Self {
14-
status: status.code(),
19+
use std::os::unix::process::ExitStatusExt as _;
20+
21+
if let Some(code) = status.code() {
22+
Self::ExitCode(code)
23+
} else if let Some(signal) = status.signal() {
24+
Self::Signal(signal)
25+
} else {
26+
Self::Unknown
27+
}
28+
}
29+
30+
#[cfg(not(unix))]
31+
pub(crate) fn new(status: std::process::ExitStatus) -> Self {
32+
if let Some(code) = status.code() {
33+
Self::ExitCode(code)
34+
} else {
35+
Self::Unknown
1536
}
1637
}
1738

1839
pub fn code(&self) -> i32 {
19-
self.status.unwrap_or(1)
40+
match self {
41+
Self::ExitCode(code) => *code,
42+
Self::Signal(signal) => 128 + *signal,
43+
Self::Unknown => 1,
44+
}
45+
.min(255)
2046
}
2147
}
2248

2349
impl std::error::Error for ExitStatusError {}
2450

2551
impl std::fmt::Display for ExitStatusError {
2652
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27-
let _ = write!(f, "subprocess exited with status: ");
28-
if let Some(status) = self.status {
29-
writeln!(f, "{status}")
30-
} else {
31-
writeln!(f, "unknown")
53+
let _ = write!(f, "subprocess exited with ");
54+
55+
match self {
56+
Self::ExitCode(code) => write!(f, "exit code: {code}"),
57+
Self::Signal(signal) => write!(f, "signal: {signal}"),
58+
Self::Unknown => write!(f, "unknown status"),
3259
}
3360
}
3461
}

0 commit comments

Comments
 (0)