Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
add test and support passing in an error
  • Loading branch information
mafintosh committed Mar 15, 2018
commit aa5498bf7f4b4c28ec007e5d269f7a240e8cdeb5
3 changes: 2 additions & 1 deletion doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,11 @@ This API can be called even if there is a pending JavaScript exception.
added: REPLACEME
-->
```C
napi_fatal_exception(napi_env env);
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.
Expand Down
30 changes: 21 additions & 9 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ struct napi_env__ {
(out) = v8::type::New((buffer), (byte_offset), (length)); \
} while (0)

#define TRIGGER_FATAL_EXCEPTION(env, local_err) \
do { \
v8::Local<v8::Message> local_msg = v8::Exception::CreateMessage( \
env->isolate, (local_err)); \
node::FatalException((env)->isolate, (local_err), local_msg); \
} while (0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final nit: We generally prefer inline functions to preprocessor macros :)



namespace {
namespace v8impl {

Expand Down Expand Up @@ -954,14 +962,14 @@ 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_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);
TRIGGER_FATAL_EXCEPTION(env, local_err);

return napi_clear_last_error(env);
}

NAPI_NO_RETURN void napi_fatal_error(const char* location,
Expand Down Expand Up @@ -3363,7 +3371,11 @@ 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.)
napi_fatal_exception(env);
if (!env->last_exception.IsEmpty()) {
v8::Local<v8::Value> local_err = v8::Local<v8::Value>::New(
env->isolate, env->last_exception);
TRIGGER_FATAL_EXCEPTION(env, local_err);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ 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_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,
Expand Down
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)