Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

errors caught from n-api addons shouldn't trigger UncaughtException reports #133

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# Build directories
/build/
/node_modules/
/test/*/build/

# Package files
NodeReport*.txt
Expand Down
17 changes: 17 additions & 0 deletions test/nan_throw/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"targets": [
{
"target_name": "test_nan_throw",
"include_dirs": [ '<!(node -e "require(\'nan\')")' ],
"conditions": [
["OS=='linux'", {
"defines": [ "_GNU_SOURCE" ],
"cflags": [ "-g", "-O2", "-std=c++11", ],
}],
],
"sources": [
"test_nan_throw.cc"
]
}
]
}
5 changes: 5 additions & 0 deletions test/nan_throw/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/nan_throw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test_nan_throw",
"version": "1.0.0",
"description": "Throw an error.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"install": "node-gyp rebuild"
},
"author": "",
"license": "ISC",
"repository": "https://github.com/nodejs/node-report",
"gypfile": true
}
13 changes: 13 additions & 0 deletions test/nan_throw/test_nan_throw.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <assert.h>

#include <nan.h>

NAN_METHOD(ThrowError) {
Nan::ThrowError("an error occurred");
}

NAN_MODULE_INIT(Init) {
Nan::SetMethod(target, "throwError", ThrowError);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
10 changes: 10 additions & 0 deletions test/napi_throw/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targets": [
{
"target_name": "test_napi_throw",
"sources": [
"test_napi_throw.c"
]
}
]
}
5 changes: 5 additions & 0 deletions test/napi_throw/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/napi_throw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test_napi_throw",
"version": "1.0.0",
"description": "Throw an error.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"install": "node-gyp rebuild"
},
"author": "",
"license": "ISC",
"repository": "https://github.com/nodejs/node-report",
"gypfile": true
}
28 changes: 28 additions & 0 deletions test/napi_throw/test_napi_throw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <assert.h>

#include <node_api.h>

napi_value ThrowError(napi_env env, napi_callback_info info) {
napi_throw_error(env, NULL, "an error occurred");
return NULL;
}

static napi_value Init(napi_env env, napi_value exports) {
napi_value result;
assert(napi_create_object(env, &result) == napi_ok);
richardlau marked this conversation as resolved.
Show resolved Hide resolved
napi_value exported_function;
assert(napi_create_function(env,
"throwError",
NAPI_AUTO_LENGTH,
ThrowError,
NULL,
&exported_function) == napi_ok);

assert(napi_set_named_property(env,
result,
"throwError",
exported_function) == napi_ok);
return result;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
22 changes: 22 additions & 0 deletions test/test-nan-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const tap = require('tap');

// Build addon that throws an error
const cp = require('child_process');
const path = require('path');
const build = cp.spawnSync('npm', [ 'install', '.',],
{ cwd: path.join(__dirname, 'nan_throw'),
encoding: 'utf8' });
tap.equal(build.stderr, '', 'No errors building addon');
tap.equal(build.signal, null, 'Failed to build addon');
tap.equal(build.status, 0, 'Failed to build addon');

// Test catching the error does not trigger an UncaughtException report
const common = require('./common.js');
const report = require('../');
const obj = require('./nan_throw/build/Release/test_nan_throw.node');
tap.throws(obj.throwError);

const reports = common.findReports(process.pid);
tap.same(reports, [], 'No reports should be generated');
22 changes: 22 additions & 0 deletions test/test-napi-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const tap = require('tap');

// Build addon that throws an error
const cp = require('child_process');
const path = require('path');
const build = cp.spawnSync('npm', [ 'install', '.' ],
{ cwd: path.join(__dirname, 'napi_throw'),
encoding: 'utf8' });
tap.equal(build.stderr, '', 'No errors building addon');
tap.equal(build.signal, null, 'Failed to build addon');
tap.equal(build.status, 0, 'Failed to build addon');

// Test catching the error does not trigger an UncaughtException report
const common = require('./common.js');
const report = require('../');
const obj = require('./napi_throw/build/Release/test_napi_throw.node');
tap.throws(obj.throwError);

const reports = common.findReports(process.pid);
tap.same(reports, [], 'No reports should be generated');