Skip to content

Commit

Permalink
Enhance error handling by introducing ExecError struct for detailed e…
Browse files Browse the repository at this point in the history
…xecution errors (#79)
  • Loading branch information
lum1n0us authored Nov 5, 2024
1 parent ac3532c commit 0ed8969
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use std::ffi::CString;
use wamr_sys::{
wasm_exec_env_t, wasm_func_get_result_count, wasm_func_get_result_types, wasm_function_inst_t,
wasm_runtime_call_wasm, wasm_runtime_get_exception, wasm_runtime_get_exec_env_singleton,
wasm_runtime_lookup_function, wasm_valkind_enum_WASM_F32, wasm_valkind_enum_WASM_F64,
wasm_valkind_enum_WASM_I32, wasm_valkind_enum_WASM_I64, wasm_valkind_t,
wasm_runtime_get_wasi_exit_code, wasm_runtime_lookup_function, wasm_valkind_enum_WASM_F32,
wasm_valkind_enum_WASM_F64, wasm_valkind_enum_WASM_I32, wasm_valkind_enum_WASM_I64,
wasm_valkind_t,
};

use crate::{helper::exception_to_string, instance::Instance, value::WasmValue, RuntimeError};
use crate::{
helper::exception_to_string, instance::Instance, value::WasmValue, ExecError, RuntimeError,
};

pub struct Function {
function: wasm_function_inst_t,
Expand Down Expand Up @@ -95,9 +98,11 @@ impl Function {
if !call_result {
unsafe {
let exception_c = wasm_runtime_get_exception(instance.get_inner_instance());
return Err(RuntimeError::ExecutionError(exception_to_string(
exception_c,
)));
let error_info = ExecError {
message: exception_to_string(exception_c),
exit_code: wasm_runtime_get_wasi_exit_code(instance.get_inner_instance()),
};
return Err(RuntimeError::ExecutionError(error_info));
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ pub mod runtime;
pub mod value;
pub mod wasi_context;

#[derive(Debug)]
pub struct ExecError {
pub message: String,
pub exit_code: u32,
}

/// all kinds of exceptions raised by WAMR
#[derive(Debug)]
pub enum RuntimeError {
Expand All @@ -168,7 +174,7 @@ pub enum RuntimeError {
/// instantiation failure
InstantiationFailure(String),
/// Error during execute wasm functions
ExecutionError(String),
ExecutionError(ExecError),
/// usually returns by `find_export_func()`
FunctionNotFound,
}
Expand All @@ -181,7 +187,11 @@ impl fmt::Display for RuntimeError {
RuntimeError::WasmFileFSError(e) => write!(f, "Wasm file operation error: {}", e),
RuntimeError::CompilationError(e) => write!(f, "Wasm compilation error: {}", e),
RuntimeError::InstantiationFailure(e) => write!(f, "Wasm instantiation failure: {}", e),
RuntimeError::ExecutionError(e) => write!(f, "Wasm execution error: {}", e),
RuntimeError::ExecutionError(info) => write!(
f,
"Wasm execution error: {} and {}",
info.message, info.exit_code
),
RuntimeError::FunctionNotFound => write!(f, "Function not found"),
}
}
Expand Down

0 comments on commit 0ed8969

Please sign in to comment.