Skip to content

Commit

Permalink
n-api: add napi_fatal_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Mar 14, 2018
1 parent e602e9c commit a575495
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 13 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,19 @@ 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: TBA
-->
```C
napi_fatal_exception(napi_env env);
```

- `[in] env`: The environment that the API is invoked under.

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
18 changes: 11 additions & 7 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ napi_status napi_get_last_error_info(napi_env env,
return napi_ok;
}

void napi_fatal_exception(napi_env env) {
if (!env->last_exception.IsEmpty()) {
v8::Local<v8::Value> err = v8::Local<v8::Value>::New(
env->isolate, env->last_exception);
v8::Local<v8::Message> msg = v8::Exception::CreateMessage(
env->isolate, err);
node::FatalException(env->isolate, err, msg);
}
}

NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down Expand Up @@ -3353,13 +3363,7 @@ class Work : public node::AsyncResource {
// If there was an unhandled exception in the complete callback,
// report it as a fatal exception. (There is no JavaScript on the
// callstack that can possibly handle it.)
if (!env->last_exception.IsEmpty()) {
v8::Local<v8::Value> err = v8::Local<v8::Value>::New(
env->isolate, env->last_exception);
v8::Local<v8::Message> msg = v8::Exception::CreateMessage(
env->isolate, err);
node::FatalException(env->isolate, err, msg);
}
napi_fatal_exception(env);
}
}

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 void napi_fatal_exception(napi_env env);

NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down

0 comments on commit a575495

Please sign in to comment.