-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
n-api: add code parameter to error helpers #13988
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few comments, but generally thanks for picking this up!
I am not sure what to do about the API breakage either.
src/node_api.cc
Outdated
static napi_status set_error_code(napi_env env, | ||
napi_value code, | ||
const char* code_cstring, | ||
v8::Local<v8::Value> val) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think this might be clearer if val
were the second parameter, that’s what I would expect for a setting operation (I’d also call it obj
, or even error
– val
is pretty generic)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I will update.
src/node_api.cc
Outdated
v8::Isolate* isolate = env->isolate; | ||
v8::Local<v8::Context> context = isolate->GetCurrentContext(); | ||
|
||
v8::MaybeLocal<v8::Object> maybe_obj = val->ToObject(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I’d prefer if this was a val->IsObject()
check + casting the handle via .As<Object>()
rather than doing a possible conversion with ToObject()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we even need the IsObject() check since for all of the callers we know we are passing in an Object so that does make the code more compact.
src/node_api.cc
Outdated
napi_throw_type_error(env, "constructor must be a function"); | ||
napi_throw_type_error(env, | ||
"ERR_NAPI_CONS_FUNCTION", | ||
"TypeError [Constructor must be a function]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it gets a bit tricky here. If I read this correctly, the resulting error will look something like TypeError: TypeError [Constructor must be a function]
when stringified; if this were an internal error thrown from JS, it should look something like TypeError[ERR_NAPI_CONS_FUNCTION]: Constructor must be a function
.
Node’s internal errors do getter magic that would not be trivial to add here, but can I suggest that to make them look more alike, set_error_code
also sets err.name
to (Type|Range)Error[$code]
in addition to setting err.code
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized that I had these wrong so came back to take a look and see you already caught it:). I had meant to make them look the same ie as you have shown 'TypeError [ERR_NAPI_CONS_FUNCTION] Constructor must be a function'. but got it wrong. Will fix it up.
Why are some error code parameters Regarding breakage at this point, go for it. That is after all the point of having an experimental API and improving it. |
@kkoopa, the codes are strings as defined in the approach outlined in #11273 I stuck with keeping the parameters consistent with the helpers. The throw_ ones take a cstring to make it easier on the callers avoiding the extra call to create a napi_value. In that case I kept the code to a cstring in that case as well. The create_ helpers used napi_value. napi_value is more generic as it support different types of strings (utf8, latin1, etc.) so I kept that flexibility of a napi_value for the code as well since it was already there |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Largely rubber stamp LGTM
@addaleax, pushed commit to address comments. |
@addaleax wondering if you are going to be able to take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ping, LGTM!
doc/api/n-api.md
Outdated
is 'ERR_ERROR_1' and a TypeError is being created the name will be: | ||
|
||
``` | ||
TypeError [ERR_ERROR_1'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ' is a typo here
Fixed typo, new CI run: https://ci.nodejs.org/job/node-test-pull-request/9073/console |
CI test failures are all the known issues after snapshots were turned off. I thin this is ready to land. NOT marking as SemVer major since N-API is still experimental. |
PR to match up the wrapper in order to minimize time wrapper is broken: nodejs/node-addon-api#78 |
needs a rebase now doing that. |
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. Fixes: nodejs#13933
adf6532
to
28ea89f
Compare
Simple rebase pushed |
Going to land. |
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. PR-URL: #13988 Fixes: #13933 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Landed as ac41db4 |
@mhdawson Did you forget to close this? |
I think so. :) |
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. PR-URL: nodejs#13988 Fixes: nodejs#13933 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. Backport-PR-URL: nodejs#14453 Backport-Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs#13988 Fixes: nodejs#13933 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
V8 6.0: The V8 engine has been upgraded to version 6.0, which has a significantly changed performance profile. [#14574](#14574) More detailed information on performance differences can be found at https://medium.com/the-node-js-collection/get-ready-a-new-v8-is-coming-node-js-performance-is-changing-46a63d6da4de Other notable changes: * **DNS** * Independent DNS resolver instances are supported now, with support for cancelling the corresponding requests. [#14518](#14518) * **N-API** * Multiple N-API functions for error handling have been changed to support assigning error codes. [#13988](#13988) * **REPL** * Autocompletion support for `require()` has been improved. [#14409](#14409) * **Utilities** * The WHATWG Encoding Standard (`TextDecoder` and `TextEncoder`) has been implemented as an experimental feature. [#13644](#13644) * **Added new collaborators** * [XadillaX](https://github.com/XadillaX) – Khaidi Chu
V8 6.0: The V8 engine has been upgraded to version 6.0, which has a significantly changed performance profile. [#14574](#14574) More detailed information on performance differences can be found at https://medium.com/the-node-js-collection/get-ready-a-new-v8-is-coming-node-js-performance-is-changing-46a63d6da4de Other notable changes: * **DNS** * Independent DNS resolver instances are supported now, with support for cancelling the corresponding requests. [#14518](#14518) * **N-API** * Multiple N-API functions for error handling have been changed to support assigning error codes. [#13988](#13988) * **REPL** * Autocompletion support for `require()` has been improved. [#14409](#14409) * **Utilities** * The WHATWG Encoding Standard (`TextDecoder` and `TextEncoder`) has been implemented as an experimental feature. [#13644](#13644) * **Added new collaborators** * [XadillaX](https://github.com/XadillaX) – Khaidi Chu
V8 6.0: The V8 engine has been upgraded to version 6.0, which has a significantly changed performance profile. [#14574](#14574) More detailed information on performance differences can be found at https://medium.com/the-node-js-collection/get-ready-a-new-v8-is-coming-node-js-performance-is-changing-46a63d6da4de Other notable changes: * **DNS** * Independent DNS resolver instances are supported now, with support for cancelling the corresponding requests. [#14518](#14518) * **N-API** * Multiple N-API functions for error handling have been changed to support assigning error codes. [#13988](#13988) * **REPL** * Autocompletion support for `require()` has been improved. [#14409](#14409) * **Utilities** * The WHATWG Encoding Standard (`TextDecoder` and `TextEncoder`) has been implemented as an experimental feature. [#13644](#13644) * **Added new collaborators** * [XadillaX](https://github.com/XadillaX) – Khaidi Chu
V8 6.0: The V8 engine has been upgraded to version 6.0, which has a significantly changed performance profile. [#14574](#14574) More detailed information on performance differences can be found at https://medium.com/the-node-js-collection/get-ready-a-new-v8-is-coming-node-js-performance-is-changing-46a63d6da4de Other notable changes: * **DNS** * Independent DNS resolver instances are supported now, with support for cancelling the corresponding requests. [#14518](#14518) * **N-API** * Multiple N-API functions for error handling have been changed to support assigning error codes. [#13988](#13988) * **REPL** * Autocompletion support for `require()` has been improved. [#14409](#14409) * **Utilities** * The WHATWG Encoding Standard (`TextDecoder` and `TextEncoder`) has been implemented as an experimental feature. [#13644](#13644) * **Added new collaborators** * [XadillaX](https://github.com/XadillaX) – Khaidi Chu * [gabrielschulhof](https://github.com/gabrielschulhof) – Gabriel Schulhof
V8 6.0: The V8 engine has been upgraded to version 6.0, which has a significantly changed performance profile. [#14574](#14574) More detailed information on performance differences can be found at https://medium.com/the-node-js-collection/get-ready-a-new-v8-is-coming-node-js-performance-is-changing-46a63d6da4de Other notable changes: * **DNS** * Independent DNS resolver instances are supported now, with support for cancelling the corresponding requests. [#14518](#14518) * **N-API** * Multiple N-API functions for error handling have been changed to support assigning error codes. [#13988](#13988) * **REPL** * Autocompletion support for `require()` has been improved. [#14409](#14409) * **Utilities** * The WHATWG Encoding Standard (`TextDecoder` and `TextEncoder`) has been implemented as an experimental feature. [#13644](#13644) * **Added new collaborators** * [XadillaX](https://github.com/XadillaX) – Khaidi Chu * [gabrielschulhof](https://github.com/gabrielschulhof) – Gabriel Schulhof Conflicts: src/node_version.h
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. PR-URL: nodejs#13988 Fixes: nodejs#13933 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
In support of the effort to add error codes to all errors generated by Node.js, add an optional code parameter to the helper functions used to throw/create errors in N-API. Backport-PR-URL: #19447 PR-URL: #13988 Fixes: #13933 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
In support of the effort to add error codes to all errors
generated by Node.js, add an optional code parameter to the
helper functions used to throw/create errors in N-API.
Fixes: #13933
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
n-api
This does assume that we are ok changing the existing APIs, that will require that we update the wrapper and our ports. The alternative would be to add new functions (maybe _with_code) instead. I lean towards the replace as it does the most to encourage the use of the code, but do want everybody's feedback on the right way to go at this point.