Skip to content

Commit a575495

Browse files
committed
n-api: add napi_fatal_exception
1 parent e602e9c commit a575495

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

doc/api/n-api.md

+13
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,19 @@ 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: TBA
547+
-->
548+
```C
549+
napi_fatal_exception(napi_env env);
550+
```
551+
552+
- `[in] env`: The environment that the API is invoked under.
553+
554+
Trigger an `uncaughtException` in JavaScript. Useful if an async
555+
callback throws an exception with no way to recover.
556+
544557
### Fatal Errors
545558

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

src/node_api.cc

+11-7
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,16 @@ napi_status napi_get_last_error_info(napi_env env,
954954
return napi_ok;
955955
}
956956

957+
void napi_fatal_exception(napi_env env) {
958+
if (!env->last_exception.IsEmpty()) {
959+
v8::Local<v8::Value> err = v8::Local<v8::Value>::New(
960+
env->isolate, env->last_exception);
961+
v8::Local<v8::Message> msg = v8::Exception::CreateMessage(
962+
env->isolate, err);
963+
node::FatalException(env->isolate, err, msg);
964+
}
965+
}
966+
957967
NAPI_NO_RETURN void napi_fatal_error(const char* location,
958968
size_t location_len,
959969
const char* message,
@@ -3353,13 +3363,7 @@ class Work : public node::AsyncResource {
33533363
// If there was an unhandled exception in the complete callback,
33543364
// report it as a fatal exception. (There is no JavaScript on the
33553365
// callstack that can possibly handle it.)
3356-
if (!env->last_exception.IsEmpty()) {
3357-
v8::Local<v8::Value> err = v8::Local<v8::Value>::New(
3358-
env->isolate, env->last_exception);
3359-
v8::Local<v8::Message> msg = v8::Exception::CreateMessage(
3360-
env->isolate, err);
3361-
node::FatalException(env->isolate, err, msg);
3362-
}
3366+
napi_fatal_exception(env);
33633367
}
33643368
}
33653369

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 void napi_fatal_exception(napi_env env);
116+
115117
NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
116118
size_t location_len,
117119
const char* message,

0 commit comments

Comments
 (0)