diff --git a/crates/common/src/result.rs b/crates/common/src/result.rs index 91c381a0..eec103de 100644 --- a/crates/common/src/result.rs +++ b/crates/common/src/result.rs @@ -101,6 +101,7 @@ impl From for WasmErrorInner { #[rustfmt::skip] pub struct WasmError { pub file: String, + pub module_path: String, pub line: u32, pub error: WasmErrorInner, } @@ -142,6 +143,7 @@ macro_rules! wasm_error { // // To remedy this we normalize the formatting here. file: file!().replace('\\', "/").to_string(), + module_path: module_path!().to_string(), line: line!(), error: $e.into(), } diff --git a/crates/host/src/error.rs b/crates/host/src/error.rs index 01f7d38c..e40701ef 100644 --- a/crates/host/src/error.rs +++ b/crates/host/src/error.rs @@ -39,6 +39,7 @@ macro_rules! wasm_host_error { // // To remedy this we normalize the formatting here. file: file!().replace('\\', "/").to_string(), + module_path: module_path!().to_string(), line: line!(), error: $e.into(), }) diff --git a/crates/host/src/guest.rs b/crates/host/src/guest.rs index 57228728..da49f150 100644 --- a/crates/host/src/guest.rs +++ b/crates/host/src/guest.rs @@ -204,7 +204,12 @@ where _ => return Err(wasm_error!(WasmErrorInner::PointerMap).into()), }, Err(e) => match e.downcast::() { - Ok(WasmError { file, line, error }) => match error { + Ok(WasmError { + file, + line, + error, + module_path + }) => match error { WasmErrorInner::HostShortCircuit(encoded) => { return match holochain_serialized_bytes::decode(&encoded) { Ok(v) => Ok(v), @@ -219,7 +224,15 @@ where } } } - _ => return Err(WasmHostError(WasmError { file, line, error }).into()), + _ => { + return Err(WasmHostError(WasmError { + file, + line, + error, + module_path, + }) + .into()) + } }, Err(e) => return Err(wasm_error!(WasmErrorInner::CallError(e.to_string())).into()), }, diff --git a/test-crates/tests/src/test.rs b/test-crates/tests/src/test.rs index c60e682e..2d5327ae 100644 --- a/test-crates/tests/src/test.rs +++ b/test-crates/tests/src/test.rs @@ -392,6 +392,7 @@ pub mod tests { Err(runtime_error) => assert_eq!( WasmError { file: "test-crates/wasms/wasm_core/src/wasm.rs".into(), + module_path: "test_wasm_core".into(), line: 102, error: WasmErrorInner::Guest("oh no!".into()), }, @@ -433,6 +434,7 @@ pub mod tests { assert_eq!( WasmError { file: "test-crates/wasms/wasm_core/src/wasm.rs".into(), + module_path: "test_wasm_core".into(), line: 130, error: WasmErrorInner::Guest("it fails!: ()".into()), }, @@ -682,4 +684,29 @@ pub mod tests { assert!(res.is_ok()); } + + #[test] + fn wasm_error_fmt() { + let InstanceWithStore { + store: store_fail, + instance: instance_fail, + } = TestWasm::Core.instance(); + + let res: Result<(), wasmer::RuntimeError> = guest::call( + &mut store_fail.lock().as_store_mut(), + instance_fail, + "try_ptr_fails_fast", + (), + ); + + assert_eq!( + format!("{}", res.clone().err().unwrap()), + "RuntimeError: WasmError { file: \"test-crates/wasms/wasm_core/src/wasm.rs\", module_path: \"test_wasm_core\", line: 130, error: Guest(\"it fails!: ()\") }" + ); + + assert_eq!( + format!("{:?}", res.err().unwrap()), + "RuntimeError { source: User(WasmError { file: \"test-crates/wasms/wasm_core/src/wasm.rs\", module_path: \"test_wasm_core\", line: 130, error: Guest(\"it fails!: ()\") }), wasm_trace: [] }" + ); + } }