Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate file paths in WasmError #140

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/common/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl From<SerializedBytesError> for WasmErrorInner {
#[rustfmt::skip]
pub struct WasmError {
pub file: String,
pub module_path: String,
pub line: u32,
pub error: WasmErrorInner,
}
Expand Down Expand Up @@ -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(),
}
Expand Down
1 change: 1 addition & 0 deletions crates/host/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
17 changes: 15 additions & 2 deletions crates/host/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ where
_ => return Err(wasm_error!(WasmErrorInner::PointerMap).into()),
},
Err(e) => match e.downcast::<WasmError>() {
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),
Expand All @@ -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()),
},
Expand Down
27 changes: 27 additions & 0 deletions test-crates/tests/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
Expand Down Expand Up @@ -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()),
},
Expand Down Expand Up @@ -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: [] }"
);
}
}
Loading