Skip to content

Commit d27e6b6

Browse files
committed
- isolate and handle unix error
Signed-off-by: Aminu 'Seun Joshua <[email protected]> - refactor: separate error code Signed-off-by: Aminu 'Seun Joshua <[email protected]> - wasm target included + update to error convention Signed-off-by: Aminu 'Seun Joshua <[email protected]> - fix: build workflow Signed-off-by: Aminu 'Seun Joshua <[email protected]> - fix: template issue with zig + docs Signed-off-by: Aminu 'Seun Joshua <[email protected]> - revert zig template Signed-off-by: Aminu 'Seun Joshua <[email protected]> - specify zig version for ci Signed-off-by: Aminu 'Seun Joshua <[email protected]> - revert workflow + re-word response Signed-off-by: Aminu 'Seun Joshua <[email protected]> - revert version edit Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent 36a4fcc commit d27e6b6

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ jobs:
2525
runs-on: "ubuntu-22.04"
2626
if: ${{ github.event_name == 'pull_request' }}
2727
steps:
28-
- name: 'Checkout Repository'
28+
- name: "Checkout Repository"
2929
uses: actions/checkout@v4
30-
- name: 'Dependency Review'
30+
- name: "Dependency Review"
3131
uses: actions/dependency-review-action@v4
3232

3333
lint-rust:
@@ -83,14 +83,8 @@ jobs:
8383
strategy:
8484
matrix:
8585
config:
86-
- {
87-
arch: "aarch64",
88-
target: "aarch64-unknown-linux-musl"
89-
}
90-
- {
91-
arch: "amd64",
92-
target: "x86_64-unknown-linux-musl"
93-
}
86+
- { arch: "aarch64", target: "aarch64-unknown-linux-musl" }
87+
- { arch: "amd64", target: "x86_64-unknown-linux-musl" }
9488
steps:
9589
- uses: actions/checkout@v3
9690

@@ -141,7 +135,6 @@ jobs:
141135
name: spin-${{ matrix.os }}
142136
path: target/release/spin${{ matrix.os == 'windows-latest' && '.exe' || '' }}
143137

144-
145138
test-spin:
146139
name: Test Spin
147140
runs-on: ${{ matrix.runner }}
@@ -200,14 +193,14 @@ jobs:
200193
- uses: goto-bus-stop/setup-zig@v2
201194
- uses: actions/setup-go@v4
202195
with:
203-
go-version: '1.23'
204-
cache-dependency-path: "**/go.sum" # To suppress warning: https://github.com/actions/setup-go/issues/427
196+
go-version: "1.23"
197+
cache-dependency-path: "**/go.sum" # To suppress warning: https://github.com/actions/setup-go/issues/427
205198
- uses: acifani/setup-tinygo@v2
206199
with:
207-
tinygo-version: '0.37.0'
200+
tinygo-version: "0.37.0"
208201
- uses: actions/setup-node@v3
209202
with:
210-
node-version: '22.x'
203+
node-version: "22.x"
211204
- name: Install Grain
212205
run: |
213206
wget https://github.com/grain-lang/grain/releases/download/grain-v0.6.6/grain-linux-x64

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)