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

Avoiding swallowing errors that arise in exported functions #674

Merged
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
19 changes: 19 additions & 0 deletions crates/cli/tests/dynamic_linking_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ pub fn test_dynamic_linking_with_func_without_flag() -> Result<()> {
Ok(())
}

#[test]
fn test_errors_in_exported_functions_are_correctly_reported() -> Result<()> {
let js_src = "export function foo() { throw \"Error\" }";
let wit = "
package local:main

world foo-test {
export foo: func()
}
";
let res = invoke_fn_on_generated_module(js_src, "foo", Some((wit, "foo-test")), None, None);
assert!(res
.err()
.unwrap()
.to_string()
.contains("error while executing"));
Ok(())
}

#[test]
pub fn check_for_new_imports() -> Result<()> {
// If you need to change this test, then you've likely made a breaking change.
Expand Down
20 changes: 17 additions & 3 deletions crates/core/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,24 @@ pub fn invoke_function(runtime: &Runtime, fn_module: &str, fn_name: &str) {
let mut opts = EvalOptions::default();
opts.strict = false;
opts.global = false;
this.eval_with_options::<Value<'_>, _>(js, opts)
.map_err(|e| from_js_error(this.clone(), e))
.map(|_| ())
let value = this.eval_with_options::<Value<'_>, _>(js, opts)?;

if let Some(promise) = value.as_promise() {
if cfg!(feature = "experimental_event_loop") {
// If the experimental event loop is enabled, trigger it.
promise.finish::<Value>().map(|_| ())
} else {
// Else we simply expect the promise to resolve immediately.
match promise.result() {
None => Err(to_js_error(this, anyhow!(EVENT_LOOP_ERR))),
Some(r) => r,
}
}
} else {
Ok(())
}
})
.map_err(|e| runtime.context().with(|cx| from_js_error(cx.clone(), e)))
.and_then(|_: ()| process_event_loop(runtime))
.unwrap_or_else(handle_error)
}
Expand Down
Loading