Skip to content

Commit 64f6515

Browse files
src: handle failure during error wrap of primitive
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>
1 parent dfad6b4 commit 64f6515

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

napi-inl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,22 @@ inline Error::Error(napi_env env, napi_value value)
29372937
nullptr};
29382938

29392939
status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag);
2940+
#ifdef NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
2941+
if (status == napi_pending_exception) {
2942+
// Test if the pending exception was reported because the environment is
2943+
// shutting down. We assume that a status of napi_pending_exception
2944+
// coupled with the absence of an actual pending exception means that
2945+
// the environment is shutting down. If so, we replace the
2946+
// napi_pending_exception status with napi_ok.
2947+
bool is_exception_pending = false;
2948+
status = napi_is_exception_pending(env, &is_exception_pending);
2949+
if (status == napi_ok && !is_exception_pending) {
2950+
status = napi_ok;
2951+
} else {
2952+
status = napi_pending_exception;
2953+
}
2954+
}
2955+
#endif // NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS
29402956
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");
29412957

29422958
// Create a reference on the newly wrapped object

0 commit comments

Comments
 (0)