mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00

This PR defines two new modes for the --unhandled-rejections flag. The first mode is called "throw". The "throw" mode first emits unhandledRejection. If this hook is not set, the "throw" mode will raise the unhandled rejection as an uncaught exception. The second mode is called "warn-with-error-code". The "warn-with-error-code" mode first emits unhandledRejection. If this hook is not set, the "warn-with-error-code" mode will trigger a warning and set the process's exit code to 1. The PR doesn't change the default behavior for unhandled rejections. That will come in a separate PR. Refs: https://github.com/nodejs/node/pull/33021 PR-URL: https://github.com/nodejs/node/pull/33475 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
38 lines
1,020 B
JavaScript
38 lines
1,020 B
JavaScript
// Flags: --unhandled-rejections=throw
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const Countdown = require('../common/countdown');
|
|
const assert = require('assert');
|
|
|
|
common.disableCrashOnUnhandledRejection();
|
|
|
|
// Verify that the unhandledRejection handler prevents triggering
|
|
// uncaught exceptions
|
|
|
|
const err1 = new Error('One');
|
|
|
|
const errors = [err1, null];
|
|
|
|
const ref = new Promise(() => {
|
|
throw err1;
|
|
});
|
|
// Explicitly reject `null`.
|
|
Promise.reject(null);
|
|
|
|
process.on('warning', common.mustNotCall('warning'));
|
|
process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
|
|
process.on('exit', assert.strictEqual.bind(null, 0));
|
|
process.on('uncaughtException', common.mustNotCall('uncaughtException'));
|
|
|
|
const timer = setTimeout(() => console.log(ref), 1000);
|
|
|
|
const counter = new Countdown(2, () => {
|
|
clearTimeout(timer);
|
|
});
|
|
|
|
process.on('unhandledRejection', common.mustCall((err) => {
|
|
counter.dec();
|
|
const knownError = errors.shift();
|
|
assert.deepStrictEqual(err, knownError);
|
|
}, 2));
|