|
3 | 3 | use anyhow::{anyhow, bail, Context as _, Result}; |
4 | 4 | use clap::Parser; |
5 | 5 | use once_cell::sync::Lazy; |
| 6 | +use std::ffi::OsStr; |
| 7 | +use std::path::{Component, Path, PathBuf}; |
6 | 8 | use std::sync::Arc; |
7 | 9 | use std::thread; |
8 | 10 | use std::time::Duration; |
9 | | -use std::{ |
10 | | - ffi::OsStr, |
11 | | - path::{Component, Path, PathBuf}, |
12 | | - process, |
13 | | -}; |
14 | | -use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType}; |
| 11 | +use wasmtime::{Engine, Func, Linker, Module, Store, Val, ValType}; |
15 | 12 | use wasmtime_cli_flags::{CommonOptions, WasiModules}; |
| 13 | +use wasmtime_wasi::maybe_exit_on_error; |
16 | 14 | use wasmtime_wasi::sync::{ambient_authority, Dir, TcpListener, WasiCtxBuilder}; |
17 | | -use wasmtime_wasi::I32Exit; |
18 | 15 |
|
19 | 16 | #[cfg(feature = "wasi-nn")] |
20 | 17 | use wasmtime_wasi_nn::WasiNnCtx; |
@@ -222,38 +219,10 @@ impl RunCommand { |
222 | 219 | { |
223 | 220 | Ok(()) => (), |
224 | 221 | Err(e) => { |
225 | | - // If a specific WASI error code was requested then that's |
226 | | - // forwarded through to the process here without printing any |
227 | | - // extra error information. |
228 | | - if let Some(exit) = e.downcast_ref::<I32Exit>() { |
229 | | - // Print the error message in the usual way. |
230 | | - // On Windows, exit status 3 indicates an abort (see below), |
231 | | - // so return 1 indicating a non-zero status to avoid ambiguity. |
232 | | - if cfg!(windows) && exit.0 >= 3 { |
233 | | - process::exit(1); |
234 | | - } |
235 | | - process::exit(exit.0); |
236 | | - } |
237 | | - |
238 | | - // If the program exited because of a trap, return an error code |
239 | | - // to the outside environment indicating a more severe problem |
240 | | - // than a simple failure. |
241 | | - if e.is::<Trap>() { |
242 | | - eprintln!("Error: {:?}", e); |
243 | | - |
244 | | - if cfg!(unix) { |
245 | | - // On Unix, return the error code of an abort. |
246 | | - process::exit(128 + libc::SIGABRT); |
247 | | - } else if cfg!(windows) { |
248 | | - // On Windows, return 3. |
249 | | - // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=vs-2019 |
250 | | - process::exit(3); |
251 | | - } |
252 | | - } |
253 | | - |
254 | | - // Otherwise fall back on Rust's default error printing/return |
| 222 | + // Exit the process if Wasmtime understands the error; |
| 223 | + // otherwise, fall back on Rust's default error printing/return |
255 | 224 | // code. |
256 | | - return Err(e); |
| 225 | + return Err(maybe_exit_on_error(e)); |
257 | 226 | } |
258 | 227 | } |
259 | 228 |
|
|
0 commit comments