-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to trigger and uncaught exception. Useful if an async callback throws an exception with no way to recover. Backport-PR-URL: #19447 PR-URL: #19337 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
eb29266
commit 49cd4fa
Showing
8 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const test_async = require(`./build/${common.buildType}/test_async`); | ||
|
||
process.on('uncaughtException', common.mustCall(function(err) { | ||
try { | ||
throw new Error('should not fail'); | ||
} catch (err) { | ||
assert.strictEqual(err.message, 'should not fail'); | ||
} | ||
assert.strictEqual(err.message, 'uncaught'); | ||
})); | ||
|
||
// Successful async execution and completion callback. | ||
test_async.Test(5, {}, common.mustCall(function() { | ||
throw new Error('uncaught'); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
test/addons-napi/test_fatal_exception/test_fatal_exception.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |