Skip to content

fix(ext/napi): prevent spurious TypeError from napi_coerce_to_object on null/undefined#32695

Merged
bartlomieju merged 2 commits intodenoland:mainfrom
bartlomieju:fix/napi-coerce-to-object-spurious-exception2
Mar 13, 2026
Merged

fix(ext/napi): prevent spurious TypeError from napi_coerce_to_object on null/undefined#32695
bartlomieju merged 2 commits intodenoland:mainfrom
bartlomieju:fix/napi-coerce-to-object-spurious-exception2

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

Fixes #31306
Fixes #28682

  • napi_coerce_to_object(null/undefined) was letting V8's ToObject() throw a TypeError, which got stored in env.last_exception by the napi_wrap! try-catch, then re-thrown by call_fn as a spurious unhandled rejection
  • This masked real errors in NAPI modules like rolldown — plugin errors showed "Cannot convert undefined or null to object" instead of the actual error message
  • Fix: check for null/undefined before calling to_object(), returning napi_invalid_arg without generating a V8 exception
  • Added regression test that verifies napi_coerce_to_object(null/undefined) returns an error status without leaving a pending exception

bartlomieju and others added 2 commits March 13, 2026 18:41
…on null/undefined

When `napi_coerce_to_object` was called with null or undefined, V8's
`ToObject()` would throw a TypeError ("Cannot convert undefined or null
to object"). The `napi_wrap!` macro's try-catch caught this and stored
it in `env.last_exception`. When the NAPI callback returned through
`call_fn`, this pending exception was re-thrown into V8, producing a
spurious unhandled rejection.

This caused real-world issues with NAPI modules like rolldown, where
plugin errors were masked by the spurious TypeError instead of showing
the original error message.

The fix checks for null/undefined before calling `to_object()`,
returning `napi_invalid_arg` without generating a V8 exception.

Closes denoland#31306

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bartlomieju bartlomieju requested a review from nathanwhit March 13, 2026 18:32
Copy link
Copy Markdown
Member

@nathanwhit nathanwhit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju merged commit 7607cb2 into denoland:main Mar 13, 2026
112 checks passed
@bartlomieju bartlomieju deleted the fix/napi-coerce-to-object-spurious-exception2 branch March 13, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic on exception mapping npm:vosk errors

2 participants