mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 15:35:41 +00:00

This adds a flag to define the default behavior for unhandled rejections. Three modes exist: `none`, `warn` and `strict`. The first is going to silence all unhandled rejection warnings. The second behaves identical to the current default with the excetion that no deprecation warning will be printed and the last is going to throw an error for each unhandled rejection, just as regular exceptions do. It is possible to intercept those with the `uncaughtException` hook as with all other exceptions as well. This PR has no influence on the existing `unhandledRejection` hook. If that is used, it will continue to function as before. PR-URL: https://github.com/nodejs/node/pull/26599 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
// Flags: --unhandled-rejections=strict
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const Countdown = require('../common/countdown');
|
|
const assert = require('assert');
|
|
|
|
common.disableCrashOnUnhandledRejection();
|
|
|
|
// Verify that unhandled rejections always trigger uncaught exceptions instead
|
|
// of triggering unhandled rejections.
|
|
|
|
const err1 = new Error('One');
|
|
const err2 = new Error(
|
|
'This error originated either by throwing ' +
|
|
'inside of an async function without a catch block, or by rejecting a ' +
|
|
'promise which was not handled with .catch(). The promise rejected with the' +
|
|
' reason "null".'
|
|
);
|
|
err2.code = 'ERR_UNHANDLED_REJECTION';
|
|
Object.defineProperty(err2, 'name', {
|
|
value: 'UnhandledPromiseRejection',
|
|
writable: true,
|
|
configurable: true
|
|
});
|
|
|
|
const errors = [err1, err2];
|
|
const identical = [true, false];
|
|
|
|
const ref = new Promise(() => {
|
|
throw err1;
|
|
});
|
|
// Explicitly reject `null`.
|
|
Promise.reject(null);
|
|
|
|
process.on('warning', common.mustNotCall('warning'));
|
|
process.on('unhandledRejection', common.mustCall(2));
|
|
process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
|
|
process.on('exit', assert.strictEqual.bind(null, 0));
|
|
|
|
const timer = setTimeout(() => console.log(ref), 1000);
|
|
|
|
const counter = new Countdown(2, () => {
|
|
clearTimeout(timer);
|
|
});
|
|
|
|
process.on('uncaughtException', common.mustCall((err, origin) => {
|
|
counter.dec();
|
|
assert.strictEqual(origin, 'unhandledRejection', err);
|
|
const knownError = errors.shift();
|
|
assert.deepStrictEqual(err, knownError);
|
|
// Check if the errors are reference equal.
|
|
assert(identical.shift() ? err === knownError : err !== knownError);
|
|
}, 2));
|