Pass exceptions to JS that are thrown by JS functions in an async context #36402
Description
Is your feature request related to a problem? Please describe.
When invoking an async function, the only way to notify JS of exceptions thrown by the function is by checking the result of every async JS function call and invoking napi_fatal_exception
if needed. It would be great if napi_fatal_exception
was called automatically when exceptions propagate out of an async function.
Describe the solution you'd like
I would like the invoker of napi_threadsafe_function_call_js
(which is passed into napi_create_threadsafe_function
) to detect when exceptions bubble out of the async callback and call napi_fatal_exception(env, error);
Currently, the napi_threadsafe_function_call_js
is invoked by node_napi_env::CallIntoModule
which runs the code
call(this); // invokes the napi_threadsafe_function_call_js
// ...
if (!last_exception.IsEmpty()) {
handle_exception(this, last_exception.Get(this->isolate));
last_exception.Reset();
}
where handle_exception
by default calls v8::Isolate::ThrowException
which does nothing in an async context.
Describe alternatives you've considered
Perhaps the real problem here is that v8::Isolate::ThrowException
does not propagate exceptions to JS when invoked in an async context? Perhaps this is a bug with v8?