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

n-api: add code parameter to error helpers #13988

Closed
wants to merge 1 commit into from

Conversation

mhdawson
Copy link
Member

@mhdawson mhdawson commented Jun 29, 2017

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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected 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.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. errors Issues and PRs related to JavaScript errors originated in Node.js core. node-api Issues and PRs related to the Node-API. labels Jun 29, 2017
@addaleax addaleax added the semver-minor PRs that contain new features and should be released in the next minor version. label Jun 29, 2017
Copy link
Member

@addaleax addaleax left a 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) {
Copy link
Member

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 errorval is pretty generic)

Copy link
Member Author

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);
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok sure.

Copy link
Member Author

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]");
Copy link
Member

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?

Copy link
Member Author

@mhdawson mhdawson Jun 29, 2017

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.

@kkoopa
Copy link

kkoopa commented Jun 29, 2017

Why are some error code parameters napi_value and others char *? I would have expected error codes to be integer.

Regarding breakage at this point, go for it. That is after all the point of having an experimental API and improving it.

@mhdawson
Copy link
Member Author

@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
for the message (ie if you have to create one napi-value then why not 2)

Copy link
Member

@jasnell jasnell left a 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

@mhdawson
Copy link
Member Author

@addaleax, pushed commit to address comments.

@mhdawson
Copy link
Member Author

mhdawson commented Jul 7, 2017

@addaleax wondering if you are going to be able to take another look.

Copy link
Member

@addaleax addaleax left a 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']
Copy link
Member

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

@jasnell
Copy link
Member

jasnell commented Jul 10, 2017

@mhdawson
Copy link
Member Author

@mhdawson
Copy link
Member Author

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.

@mhdawson
Copy link
Member Author

PR to match up the wrapper in order to minimize time wrapper is broken: nodejs/node-addon-api#78

@mhdawson
Copy link
Member Author

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

Simple rebase pushed

@mhdawson
Copy link
Member Author

Going to land.

mhdawson added a commit that referenced this pull request Jul 13, 2017
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@mhdawson
Copy link
Member Author

Landed as ac41db4

@jasongin
Copy link
Member

@mhdawson Did you forget to close this?

@addaleax
Copy link
Member

I think so. :)

@addaleax addaleax closed this Jul 24, 2017
addaleax pushed a commit to addaleax/node that referenced this pull request Jul 24, 2017
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
addaleax pushed a commit to addaleax/node that referenced this pull request Jul 27, 2017
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 <jasnell@gmail.com>

PR-URL: nodejs#13988
Fixes: nodejs#13933
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@addaleax addaleax mentioned this pull request Aug 2, 2017
@addaleax addaleax added the notable-change PRs with changes that should be highlighted in changelogs. label Aug 3, 2017
addaleax added a commit that referenced this pull request Aug 3, 2017
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
addaleax added a commit that referenced this pull request Aug 7, 2017
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
cjihrig pushed a commit that referenced this pull request Aug 8, 2017
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
addaleax added a commit that referenced this pull request Aug 9, 2017
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
cjihrig pushed a commit that referenced this pull request Aug 10, 2017
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
gabrielschulhof pushed a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this pull request Apr 16, 2018
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@MylesBorins MylesBorins mentioned this pull request Apr 16, 2018
@mhdawson mhdawson deleted the napi-error-code branch September 30, 2019 13:13
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++. errors Issues and PRs related to JavaScript errors originated in Node.js core. node-api Issues and PRs related to the Node-API. notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants