-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
test: add coverage for error apis #12729
Conversation
Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports.
test/addons-napi/test_error/test.js
Outdated
|
||
error = test_error.createTypeError(); | ||
assert.ok(error instanceof TypeError, | ||
'expected error to be an instance of TypeeError'); |
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.
TypeError
test/addons-napi/test_error/test.js
Outdated
@@ -55,3 +55,61 @@ assert.strictEqual(test_error.checkError({}), false, | |||
// Test that non-error primitive is correctly classed | |||
assert.strictEqual(test_error.checkError('non-object'), false, | |||
'Non-error primitive correctly classed by napi_is_error'); | |||
|
|||
try { | |||
test_error.throwExistingError(); |
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.
If throwExistingError()
and similar calls below don't actually throw an error, then this test would incorrectly pass, right?
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.
Right. I'd suggest using assert.throws()
to verify expected exceptions. It will make cleaner code anyway.
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.
Agreed. assert.throws()
is a much better choice here.
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.
Agreed, changing.
NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message)); | ||
NAPI_CALL(env, napi_create_error(env, message, &error)); | ||
NAPI_CALL(env, napi_throw(env, error)); | ||
return nullptr; |
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 noting for the future: it would be excellent if these n-api thrown errors followed the same conventions as the internal/errors
module. That is, include a code
property and the [{code}]:
in the name.
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.
The napi_create_error()
and napi_throw_error()
and similar APIs are intended to be used by native add-on modules to return and/or throw errors to JavaScript. So, it would be up to the native add-on code to supply meaningful values for the code
property. But, N-API could encourage conformance to that convention by providing APIs such as napi_create_error_with_code()
, napi_throw_error_with_code()
. I'll open a new issue to track that.
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.
Opened nodejs/abi-stable-node#241
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.
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.
LGTM with a nit.
test/addons-napi/test_error/test.js
Outdated
|
||
assert.throws(() => { | ||
test_error.throwExistingError(); | ||
}, /Error: existing error/); |
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.
We've been moving toward adding ^
and $
to make the regular expression stricter.
Thanks for the review, pushed commit to address comments: CI run: https://ci.nodejs.org/job/node-test-pull-request/7837/ |
CI good landing. |
landed as 94a120c |
Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports. PR-URL: #12729 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports. PR-URL: nodejs#12729 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports. PR-URL: nodejs#12729 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add coverage for N-API functions related to throwing and creating errors. A number of these are currently showing as not having any coverage in the nightly code coverage reports. Backport-PR-URL: #19447 PR-URL: #12729 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add coverage for N-API functions related to
throwing and creating errors. A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
test, n-api