-
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
Add napi_fatal_error API #13971
Add napi_fatal_error API #13971
Conversation
doc/api/n-api.md
Outdated
- `[in] location`: The location at which the error occurred. | ||
- `[in] message`: The message associated with the error. | ||
|
||
Call does not return, the process will be terminated. |
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.
Call -> The function call
@@ -772,6 +772,11 @@ napi_status napi_get_last_error_info(napi_env env, | |||
return napi_ok; | |||
} | |||
|
|||
NAPI_NO_RETURN void napi_fatal_error(const char* location, | |||
const char* message) { | |||
node::FatalError(location, message); |
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.
Doesn't this require node internals?
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.
It does, but since it was already included in the file I assumed it wasn't a big deal. Since this is the v8-specific implementation of N-API, this can still be considered internal (I think).
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.
Sorry, I forgot this hadn't landed. #13892
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.
Ahh, ok, I hadn't seen that. It seems like it would be best then to adapt an implementation into node_api.cc that mimics the node::FatalError behavior?
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.
Yes, that is what I had expected we would need to do.
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.
Based on discussion today in the WG meeting, we'll be reverting back to the original change and removing the duplicated code.
"targets": [ | ||
{ | ||
"target_name": "test_fatal", | ||
"sources": [ "test_fatal.cc" ] |
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.
Any reason not to use just C?
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
#if defined _WIN32 |
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.
Do you need windows.h
and/or unistd.h
?
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.
Nope, I meant to try removing this and forgot.
434945d
to
70e0919
Compare
src/node_api.cc
Outdated
fflush(stderr); | ||
ABORT(); | ||
// to suppress compiler warning | ||
ABORT(); |
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.
Whoops, I'll clean this up.
fc27c86
to
44bcf8e
Compare
Resolves #13927 |
test/addons-napi/test_fatal/test.js
Outdated
const test_fatal = require(`./build/${common.buildType}/test_fatal`); | ||
|
||
// Exception thrown from async completion callback. | ||
// (Tested in a spawned process because the exception is fatal.) |
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 recognize these comments, they were copy-pasted from an async callback test I wrote. Please update them. :)
src/node_api.cc
Outdated
|
||
// Don't include the null character in the output | ||
CHECK_GT(n, 0); | ||
WriteConsoleW(stderr_handle, wbuf.data(), n - 1, nullptr, 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.
Does anyone know the reason for this special case of calling WriteConsoleW()
for a windows console? node::Assert()
is very similar to node::FatalError()
, but it just does a regular fprintf(stderr, ...)
.
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.
Oh, I think it's required for proper display of UTF-8 encoded error messages? I guess assertion messages are more likely to be limited to plain ASCII. I suppose we do want UTF-8 capability here.
doc/api/n-api.md
Outdated
``` | ||
|
||
- `[in] location`: The location at which the error occurred. | ||
- `[in] message`: The message associated with the 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.
Make it clear that location
is NULL
-able, while message
isn't. Speaking of that, I'd actually prefer the function signature to be (message, location)
as it seems customary for NULL
-able parameters to go last.
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.
Speaking of this, maybe it would be helpful if message
can be a printf format string instead? That'd invalidate my comment above but would provide callers more flexibility.
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 would lead to mistakes when people are migrating code to N-API if the parameter order is different from node::FatalError()
.
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.
Copy/pasting large chunks of code from src/node.cc is not acceptable. See also #13892 (comment).
Based on discussion today in N-API meeting we are going to look at having a version of the node internals file as part of the wrapper so that we can avoid having to have copies in the core Node.js codebase. |
e8ac6ae
to
bf61c1f
Compare
945c457
to
ee3aa34
Compare
@bnoordhuis Can you take another look? I've eliminated all of the code duplication in node source files. |
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 style nit.
test/addons-napi/test_fatal/test.js
Outdated
} | ||
|
||
const p = child_process.spawnSync( | ||
process.execPath, [ '--napi-modules', __filename, 'child' ]); |
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.
4 space indent here and below (line continuation.)
}; | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_define_properties( | ||
env, exports, sizeof(properties) / sizeof(*properties), properties)); |
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.
Tangential: macros like NAPI_ASSERT_BASE and NAPI_CALL_BASE should have their bodies wrapped in do { } while (0)
in order for them to be safe to use in if/else statements. Right now they can create bugs that will be hard to figure out. Something for another PR.
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 opened PR #14095 to fix this.
4068780
to
6a32422
Compare
FreeBSD rerun: https://ci.nodejs.org/job/node-test-commit-freebsd/10188/ |
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
Rebased and new CI: https://ci.nodejs.org/job/node-test-pull-request/9113/ |
Failed in CI on PPCBE is known issue. |
Failure on Alpine is known issue. |
Linux fips issue was a machine issue. |
Ok CI completed, landing. |
PR-URL: #13971 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
landed as 73078d6 |
PR-URL: #13971 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
PR-URL: #13971 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
PR-URL: nodejs#13971 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
Backport-PR-URL: #19447 PR-URL: #13971 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
n-api