Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Oct 4, 2019

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-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Oct 4, 2019
"ERR_NAPI_CONS_FUNCTION",
"Constructor must be a function");

return napi_set_last_error(env, napi_function_expected);
Copy link
Member Author

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
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
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.

src/js_native_api_v8.h Outdated Show resolved Hide resolved
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.
@legendecas legendecas changed the title src: macro throwing error with napi_pending_exception returning src,n-api: throwing error with napi_pending_exception status Oct 10, 2019
Copy link
Member

@mhdawson mhdawson left a comment

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
Copy link
Member Author

legendecas commented Oct 12, 2019

@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.

Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

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.

"ERR_NAPI_CONS_FUNCTION",
"Constructor must be a function");

return napi_set_last_error(env, napi_function_expected);
Copy link
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.

"ERR_NAPI_CONS_FUNCTION",
"Constructor must be a function");

return napi_set_last_error(env, napi_function_expected);
Copy link
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.

@gabrielschulhof gabrielschulhof 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
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
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.

@@ -140,14 +140,22 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code,
(!try_catch.HasCaught() ? napi_ok \
: napi_set_last_error((env), napi_pending_exception))

#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
Copy link
Member Author

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
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
Member Author

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
Member Author

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 closed this Oct 18, 2019
@legendecas legendecas deleted the napi-throwing branch March 14, 2020 06:50
Sign up for free to 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-api Issues and PRs related to the Node-API. node-api-semver-major semver-major changes that need to be considered for N-API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants