• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --unhandled-rejections=throw
2'use strict';
3
4const common = require('../common');
5const Countdown = require('../common/countdown');
6const assert = require('assert');
7
8// Verify that unhandled rejections always trigger uncaught exceptions instead
9// of triggering unhandled rejections.
10
11const err1 = new Error('One');
12const err2 = new Error(
13  'This error originated either by throwing ' +
14  'inside of an async function without a catch block, or by rejecting a ' +
15  'promise which was not handled with .catch(). The promise rejected with the' +
16  ' reason "null".'
17);
18err2.code = 'ERR_UNHANDLED_REJECTION';
19Object.defineProperty(err2, 'name', {
20  value: 'UnhandledPromiseRejection',
21  writable: true,
22  configurable: true
23});
24
25const errors = [err1, err2];
26const identical = [true, false];
27
28const ref = new Promise(() => {
29  throw err1;
30});
31// Explicitly reject `null`.
32Promise.reject(null);
33
34process.on('warning', common.mustNotCall('warning'));
35// If we add an unhandledRejection handler, the exception won't be thrown
36// process.on('unhandledRejection', common.mustCall(2));
37process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
38process.on('exit', assert.strictEqual.bind(null, 0));
39
40const timer = setTimeout(() => console.log(ref), 1000);
41
42const counter = new Countdown(2, () => {
43  clearTimeout(timer);
44});
45
46process.on('uncaughtException', common.mustCall((err, origin) => {
47  counter.dec();
48  assert.strictEqual(origin, 'unhandledRejection', err);
49  const knownError = errors.shift();
50  assert.deepStrictEqual(err, knownError);
51  // Check if the errors are reference equal.
52  assert(identical.shift() ? err === knownError : err !== knownError);
53}, 2));
54