Skip to content

src,n-api: throwing error with napi_pending_exception status - #29847

Closed
legendecas wants to merge 1 commit into
nodejs:masterfrom
legendecas:napi-throwing
Closed

src,n-api: throwing error with napi_pending_exception status#29847
legendecas wants to merge 1 commit into
nodejs:masterfrom
legendecas:napi-throwing

Conversation

@legendecas

@legendecaslegendecas commented Oct 4, 2019

Copy link
Copy Markdown
Member

It should be napi_pending_exception if an JavaScript error is
pending to be handled. Or it might have a great chance to be ignored.
See https://github.com/nodejs/node-addon-api/blob/master/napi-inl.h#L2006-L2013

Related: #29768 (comment)

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-botnodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Oct 4, 2019
Comment threadsrc/js_native_api_v8.cc Outdated

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Maybe JavaScript error should not be thrown here and returning napi_function_expected instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That would be a semver-major change, I think, so we mustn't.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, we cannot change the return status from napi_function_expected to napi_pending_exception because that's semver-major as well.

Comment threadsrc/js_native_api_v8.h Outdated
It should be napi_pending_exception if an JavaScript error is
pending to be handled. Or it might have a great chance to be ignored.
@legendecaslegendecas changed the title src: macro throwing error with napi_pending_exception returningsrc,n-api: throwing error with napi_pending_exception statusOct 10, 2019

@mhdawsonmhdawson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@legendecas its hard to tell from the diff. Are the functions being changed still experimental?

@legendecas

legendecas commented Oct 12, 2019

Copy link
Copy Markdown
MemberAuthor

@mhdawson its hard to tell from the diff. Are the functions being changed still experimental?

No, they are not. The PR was trying to address the issue that thrown exceptions were possibly ignored in actual usage like in node-addon-api. Since semantically no exception were expected on non-napi_pending_exception status.

Or we could update node-addon-api to not ignore the JS exception whatever the status is. But it might be ambiguous since there might be two errors to be handled: one for the error status, one for the thrown JS exception.

@gabrielschulhofgabrielschulhof left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@legendecas Unfortunately we cannot change what status a function returns under what conditions because that would be semver-major. In this case, because we're talking about N-API, semver-major does not mean that's it's OK to release with the next major version of Node.js, but that it's OK to release with the next major version of N-API.

Let's label this PR, even if closed, to remind us of the changes we need to make if/when we decide to release a version of N-API with semver-major changes.

Comment threadsrc/js_native_api_v8.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That would be a semver-major change, I think, so we mustn't.

Comment threadsrc/js_native_api_v8.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, we cannot change the return status from napi_function_expected to napi_pending_exception because that's semver-major as well.

@gabrielschulhofgabrielschulhof added node-api Issues and PRs related to the Node-API. node-api-semver-major semver-major changes that need to be considered for N-API labels Oct 13, 2019
@gabrielschulhof

Copy link
Copy Markdown
Contributor

@legendecas I think in node-addon-api we can handle those N-API calls we know throw exceptions as special cases, and smooth over the differences in behaviour there to avoid ignoring exceptions and/or throwing double exceptions.

Unfortunately this non-uniformity has crept into N-API. We'll have to be vigilant in the future to avoid it from affecting newly added N-APIs.

@gabrielschulhof

Copy link
Copy Markdown
Contributor

@legendecas Additionally, N-APIs strong ABI stability guarantees mean that we must not change the return values of a function in response to certain error conditions.

(!try_catch.HasCaught() ? napi_ok \
: napi_set_last_error((env), napi_pending_exception))

#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \

@legendecaslegendecasOct 13, 2019

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

The only place that used this macro is napi_create_typedarray by CREATE_TYPED_ARRAY.

@mhdawson

Copy link
Copy Markdown
Member

My question about experimental status was directly related to what @gabrielschulhof has commented. We can't change the behavior of non-experimental APIs.

@legendecas

Copy link
Copy Markdown
MemberAuthor

Yes, I'd agree that changing the behavior of non-experiment N-APIs would hardly be acceptable.

Still, it would be nice to mark these macros that not aligned with the expected design goals as deprecated to prevent possible future maintenance/new-APIs from using these macros.

@legendecas

Copy link
Copy Markdown
MemberAuthor

Closing for non-experimental behavior changes and I'd like to propose another one with updating the comments or so to mark the macros as deprecated from been used in new N-APIs.

@legendecas
legendecas deleted the napi-throwing branch March 14, 2020 06:50
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++Issues and PRs that require attention from people who are familiar with C++.node-apiIssues and PRs related to the Node-API.node-api-semver-majorsemver-major changes that need to be considered for N-API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@legendecas@gabrielschulhof@mhdawson@cjihrig@nodejs-github-bot