Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e304c42
test: add unhandled rejection guard
babygoat Nov 23, 2017
887dcb9
test: remove literals that obscure assert messages
Trott Dec 13, 2017
bba1442
n-api: fix memory leak in napi_async_destroy()
Dec 17, 2017
bcbb7af
test: remove ambiguous error messages from test_error
nadrane Dec 21, 2017
6bf3769
doc: updates examples to use NULL
mhdawson Jan 5, 2018
f81330f
n-api: throw RangeError in napi_create_dataview() with invalid range
romandev Dec 26, 2017
6d23c24
n-api: expose n-api version in process.versions
mhdawson Jan 9, 2018
40ce083
doc: napi: fix unbalanced emphasis
ofrobots Jan 12, 2018
621117e
doc: napi: make header style consistent
ofrobots Jan 12, 2018
111678b
n-api: throw RangeError napi_create_typedarray()
romandev Jan 8, 2018
94618a5
test: fixed typos in napi test
furstenheim Jan 14, 2018
375d043
doc: remove uannecessary Require
mhdawson Jan 16, 2018
f7fb0c3
timers: allow Immediates to be unrefed
apapirovski Jan 13, 2018
23fd460
test: refactor addons-napi/test_exception/test.js
Trott Jan 24, 2018
021e4a4
n-api: change assert ok check to notStrictEqual.
nbdaaron Jan 27, 2018
bf96235
test: show pending exception error in napi tests
blairwilcox Jan 27, 2018
17862cd
n-api: implement wrapping using private properties
Jan 23, 2018
29bbf0c
n-api: wrap control flow macro in do/while
bnoordhuis Feb 2, 2018
6619a1b
doc: remove usage of you in n-api doc
mhdawson Feb 2, 2018
8923ad5
doc: small typo in n-api.md
iSkore Feb 4, 2018
ed8ac32
n-api: add methods to open/close callback scope
mhdawson Dec 13, 2017
01b28fa
n-api: remove extra reference from test
Feb 3, 2018
d2f0672
doc: fix typo in n-api.md
vsemozhetbyt Feb 6, 2018
9c20b87
test: improve error message output
bshankar Feb 1, 2018
ef50323
test: convert new tests to use error types
jackhorton Feb 5, 2018
c4592e6
doc: fix exporting a function example
Feb 8, 2018
5ab4d31
doc: mark NAPI_AUTO_LENGTH as code
tniessen Feb 10, 2018
a1df466
test: remove unnecessary timer
cjihrig Feb 11, 2018
eb85cd8
n-api: fix object test
Feb 27, 2018
2b3207d
doc: fix n-api asynchronous threading docs
ebickle Mar 1, 2018
7a80a8d
n-api: update reference test
Mar 2, 2018
de2d3a6
n-api: update documentation
Mar 1, 2018
a92796e
n-api: resolve promise in test
Mar 8, 2018
2cfd4fe
n-api,test: add a new.target test to addons-napi
boingoing Jan 17, 2018
4a0fc8a
n-api,test: add int64 bounds tests
kfarnung Mar 12, 2018
ff968b7
doc: fix typos on n-api
shama Mar 16, 2018
01790d4
n-api: add missing exception checking
mhdawson Mar 15, 2018
a19b829
n-api: separate out async_hooks test
Mar 16, 2018
9b1b810
n-api: add napi_fatal_exception
mafintosh Mar 14, 2018
3a50835
n-api: re-write test_make_callback
Mar 19, 2018
b25dec8
doc: Add a missing comma
leaves4j Mar 23, 2018
c33daa4
n-api: bump version of n-api supported
mhdawson Mar 20, 2018
65b06de
n-api: ensure in-module exceptions are propagated
Mar 22, 2018
e3a2737
n-api: back up env before finalize
Apr 1, 2018
d73e802
n-api: add more `int64_t` tests
kfarnung Mar 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
n-api: add napi_fatal_exception
Add function to trigger and uncaught exception.
Useful if an async callback throws an exception with
no way to recover.

PR-URL: #19337
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
mafintosh authored and Gabriel Schulhof committed Apr 12, 2018
commit 9b1b810cdc9e012f6233eefcb37a8b9bfedc1dcf
14 changes: 14 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ This API returns true if an exception is pending.

This API can be called even if there is a pending JavaScript exception.

#### napi_fatal_exception
<!-- YAML
added: REPLACEME
-->
```C
napi_status napi_fatal_exception(napi_env env, napi_value err);
```

- `[in] env`: The environment that the API is invoked under.
- `[in] err`: The error you want to pass to `uncaughtException`.

Trigger an `uncaughtException` in JavaScript. Useful if an async
callback throws an exception with no way to recover.

### Fatal Errors

In the event of an unrecoverable error in a native module, a fatal error can be
Expand Down
25 changes: 21 additions & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ struct napi_env__ {
(out) = v8::type::New((buffer), (byte_offset), (length)); \
} while (0)


namespace {
namespace v8impl {

Expand Down Expand Up @@ -295,6 +296,13 @@ v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
return local;
}

static inline void trigger_fatal_exception(
napi_env env, v8::Local<v8::Value> local_err) {
v8::Local<v8::Message> local_msg =
v8::Exception::CreateMessage(env->isolate, local_err);
node::FatalException(env->isolate, local_err, local_msg);
}

static inline napi_status V8NameFromPropertyDescriptor(napi_env env,
const napi_property_descriptor* p,
v8::Local<v8::Name>* result) {
Expand Down Expand Up @@ -971,6 +979,16 @@ napi_status napi_get_last_error_info(napi_env env,
return napi_ok;
}

napi_status napi_fatal_exception(napi_env env, napi_value err) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, err);

v8::Local<v8::Value> local_err = v8impl::V8LocalValueFromJsValue(err);
v8impl::trigger_fatal_exception(env, local_err);

return napi_clear_last_error(env);
}

NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down Expand Up @@ -3375,10 +3393,9 @@ class Work : public node::AsyncResource {
// report it as a fatal exception. (There is no JavaScript on the
// callstack that can possibly handle it.)
if (!env->last_exception.IsEmpty()) {
v8::TryCatch try_catch(env->isolate);
env->isolate->ThrowException(
v8::Local<v8::Value>::New(env->isolate, env->last_exception));
node::FatalException(env->isolate, try_catch);
v8::Local<v8::Value> local_err = v8::Local<v8::Value>::New(
env->isolate, env->last_exception);
v8impl::trigger_fatal_exception(env, local_err);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ NAPI_EXTERN napi_status
napi_get_last_error_info(napi_env env,
const napi_extended_error_info** result);

NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err);

NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down
5 changes: 5 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(err);
}

void FatalException(v8::Isolate* isolate,
v8::Local<v8::Value> error,
v8::Local<v8::Message> message);


void SignalExit(int signo);
#ifdef __POSIX__
void RegisterSignalHandler(int signal,
Expand Down
18 changes: 18 additions & 0 deletions test/addons-napi/test_async/test-uncaught.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const test_async = require(`./build/${common.buildType}/test_async`);

process.on('uncaughtException', common.mustCall(function(err) {
try {
throw new Error('should not fail');
} catch (err) {
assert.strictEqual(err.message, 'should not fail');
}
assert.strictEqual(err.message, 'uncaught');
}));

// Successful async execution and completion callback.
test_async.Test(5, {}, common.mustCall(function() {
throw new Error('uncaught');
}));
8 changes: 8 additions & 0 deletions test/addons-napi/test_fatal_exception/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "test_fatal_exception",
"sources": [ "test_fatal_exception.c" ]
}
]
}
11 changes: 11 additions & 0 deletions test/addons-napi/test_fatal_exception/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const test_fatal = require(`./build/${common.buildType}/test_fatal_exception`);

process.on('uncaughtException', common.mustCall(function(err) {
assert.strictEqual(err.message, 'fatal error');
}));

const err = new Error('fatal error');
test_fatal.Test(err);
26 changes: 26 additions & 0 deletions test/addons-napi/test_fatal_exception/test_fatal_exception.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <node_api.h>
#include "../common.h"

napi_value Test(napi_env env, napi_callback_info info) {
napi_value err;
size_t argc = 1;

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &err, NULL, NULL));

NAPI_CALL(env, napi_fatal_exception(env, err));

return NULL;
}

napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NAPI_PROPERTY("Test", Test),
};

NAPI_CALL(env, napi_define_properties(
env, exports, sizeof(properties) / sizeof(*properties), properties));

return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)