Skip to content

Commit 29a04b7

Browse files
mafintoshMylesBorins
authored andcommitted
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 <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 7458293 commit 29a04b7

File tree

8 files changed

+105
-4
lines changed

8 files changed

+105
-4
lines changed

doc/api/n-api.md

+14
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,20 @@ This API returns true if an exception is pending.
541541

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

544+
#### napi_fatal_exception
545+
<!-- YAML
546+
added: REPLACEME
547+
-->
548+
```C
549+
napi_status napi_fatal_exception(napi_env env, napi_value err);
550+
```
551+
552+
- `[in] env`: The environment that the API is invoked under.
553+
- `[in] err`: The error you want to pass to `uncaughtException`.
554+
555+
Trigger an `uncaughtException` in JavaScript. Useful if an async
556+
callback throws an exception with no way to recover.
557+
544558
### Fatal Errors
545559

546560
In the event of an unrecoverable error in a native module, a fatal error can be

src/node_api.cc

+21-4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ struct napi_env__ {
167167
(out) = v8::type::New((buffer), (byte_offset), (length)); \
168168
} while (0)
169169

170+
170171
namespace {
171172
namespace v8impl {
172173

@@ -289,6 +290,13 @@ v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
289290
return local;
290291
}
291292

293+
static inline void trigger_fatal_exception(
294+
napi_env env, v8::Local<v8::Value> local_err) {
295+
v8::Local<v8::Message> local_msg =
296+
v8::Exception::CreateMessage(env->isolate, local_err);
297+
node::FatalException(env->isolate, local_err, local_msg);
298+
}
299+
292300
static inline napi_status V8NameFromPropertyDescriptor(napi_env env,
293301
const napi_property_descriptor* p,
294302
v8::Local<v8::Name>* result) {
@@ -954,6 +962,16 @@ napi_status napi_get_last_error_info(napi_env env,
954962
return napi_ok;
955963
}
956964

965+
napi_status napi_fatal_exception(napi_env env, napi_value err) {
966+
NAPI_PREAMBLE(env);
967+
CHECK_ARG(env, err);
968+
969+
v8::Local<v8::Value> local_err = v8impl::V8LocalValueFromJsValue(err);
970+
v8impl::trigger_fatal_exception(env, local_err);
971+
972+
return napi_clear_last_error(env);
973+
}
974+
957975
NAPI_NO_RETURN void napi_fatal_error(const char* location,
958976
size_t location_len,
959977
const char* message,
@@ -3358,10 +3376,9 @@ class Work : public node::AsyncResource {
33583376
// report it as a fatal exception. (There is no JavaScript on the
33593377
// callstack that can possibly handle it.)
33603378
if (!env->last_exception.IsEmpty()) {
3361-
v8::TryCatch try_catch(env->isolate);
3362-
env->isolate->ThrowException(
3363-
v8::Local<v8::Value>::New(env->isolate, env->last_exception));
3364-
node::FatalException(env->isolate, try_catch);
3379+
v8::Local<v8::Value> local_err = v8::Local<v8::Value>::New(
3380+
env->isolate, env->last_exception);
3381+
v8impl::trigger_fatal_exception(env, local_err);
33653382
}
33663383
}
33673384
}

src/node_api.h

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ NAPI_EXTERN napi_status
112112
napi_get_last_error_info(napi_env env,
113113
const napi_extended_error_info** result);
114114

115+
NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err);
116+
115117
NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
116118
size_t location_len,
117119
const char* message,

src/node_internals.h

+5
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
248248
args.GetReturnValue().Set(err);
249249
}
250250

251+
void FatalException(v8::Isolate* isolate,
252+
v8::Local<v8::Value> error,
253+
v8::Local<v8::Message> message);
254+
255+
251256
void SignalExit(int signo);
252257
#ifdef __POSIX__
253258
void RegisterSignalHandler(int signal,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const test_async = require(`./build/${common.buildType}/test_async`);
5+
6+
process.on('uncaughtException', common.mustCall(function(err) {
7+
try {
8+
throw new Error('should not fail');
9+
} catch (err) {
10+
assert.strictEqual(err.message, 'should not fail');
11+
}
12+
assert.strictEqual(err.message, 'uncaught');
13+
}));
14+
15+
// Successful async execution and completion callback.
16+
test_async.Test(5, {}, common.mustCall(function() {
17+
throw new Error('uncaught');
18+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_fatal_exception",
5+
"sources": [ "test_fatal_exception.c" ]
6+
}
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const test_fatal = require(`./build/${common.buildType}/test_fatal_exception`);
5+
6+
process.on('uncaughtException', common.mustCall(function(err) {
7+
assert.strictEqual(err.message, 'fatal error');
8+
}));
9+
10+
const err = new Error('fatal error');
11+
test_fatal.Test(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <node_api.h>
2+
#include "../common.h"
3+
4+
napi_value Test(napi_env env, napi_callback_info info) {
5+
napi_value err;
6+
size_t argc = 1;
7+
8+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &err, NULL, NULL));
9+
10+
NAPI_CALL(env, napi_fatal_exception(env, err));
11+
12+
return NULL;
13+
}
14+
15+
napi_value Init(napi_env env, napi_value exports) {
16+
napi_property_descriptor properties[] = {
17+
DECLARE_NAPI_PROPERTY("Test", Test),
18+
};
19+
20+
NAPI_CALL(env, napi_define_properties(
21+
env, exports, sizeof(properties) / sizeof(*properties), properties));
22+
23+
return exports;
24+
}
25+
26+
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

0 commit comments

Comments
 (0)