Skip to content

Commit

Permalink
src: handle failure during error wrap of primitive
Browse files Browse the repository at this point in the history
When we wrap a primitive value into an object in order to throw it as
an error, we call `napi_define_properties()` which may return
`napi_pending_exception` if the environment is shutting down. Handle
this case when `NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS` is given by
checking whether we're in an environment shutdown scenario and ignore
the failure of `napi_define_properties()`, since the error will not
reach JS anyway.

Signed-off-by: Gabriel Schulhof <gabrielschulhof@gmail.com>
PR-URL: #1310
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
gabrielschulhof committed Apr 29, 2023
1 parent dfad6b4 commit 64f6515
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions napi-inl.h
Expand Up @@ -2937,6 +2937,22 @@ inline Error::Error(napi_env env, napi_value value)
nullptr};

status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag);
#ifdef NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
if (status == napi_pending_exception) {
// Test if the pending exception was reported because the environment is
// shutting down. We assume that a status of napi_pending_exception
// coupled with the absence of an actual pending exception means that
// the environment is shutting down. If so, we replace the
// napi_pending_exception status with napi_ok.
bool is_exception_pending = false;
status = napi_is_exception_pending(env, &is_exception_pending);
if (status == napi_ok && !is_exception_pending) {
status = napi_ok;
} else {
status = napi_pending_exception;
}
}
#endif // NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");

// Create a reference on the newly wrapped object
Expand Down

0 comments on commit 64f6515

Please sign in to comment.