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