11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Verify that unhandled rejections always trigger uncaught exceptions instead
81cb0ef41Sopenharmony_ci// of triggering unhandled rejections.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst err1 = new Error('One');
111cb0ef41Sopenharmony_ciconst err2 = new Error(
121cb0ef41Sopenharmony_ci  'This error originated either by throwing ' +
131cb0ef41Sopenharmony_ci  'inside of an async function without a catch block, or by rejecting a ' +
141cb0ef41Sopenharmony_ci  'promise which was not handled with .catch(). The promise rejected with the' +
151cb0ef41Sopenharmony_ci  ' reason "null".'
161cb0ef41Sopenharmony_ci);
171cb0ef41Sopenharmony_cierr2.code = 'ERR_UNHANDLED_REJECTION';
181cb0ef41Sopenharmony_ciObject.defineProperty(err2, 'name', {
191cb0ef41Sopenharmony_ci  value: 'UnhandledPromiseRejection',
201cb0ef41Sopenharmony_ci  writable: true,
211cb0ef41Sopenharmony_ci  configurable: true
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst errors = [err1, err2];
251cb0ef41Sopenharmony_ciconst identical = [true, false];
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst ref = new Promise(() => {
281cb0ef41Sopenharmony_ci  throw err1;
291cb0ef41Sopenharmony_ci});
301cb0ef41Sopenharmony_ci// Explicitly reject `null`.
311cb0ef41Sopenharmony_ciPromise.reject(null);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciprocess.on('warning', common.mustNotCall('warning'));
341cb0ef41Sopenharmony_ci// If we add an unhandledRejection handler, the exception won't be thrown
351cb0ef41Sopenharmony_ci// process.on('unhandledRejection', common.mustCall(2));
361cb0ef41Sopenharmony_ciprocess.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
371cb0ef41Sopenharmony_ciprocess.on('exit', assert.strictEqual.bind(null, 0));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst timer = setTimeout(() => console.log(ref), 1000);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciconst counter = new Countdown(2, () => {
421cb0ef41Sopenharmony_ci  clearTimeout(timer);
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciprocess.on('uncaughtException', common.mustCall((err, origin) => {
461cb0ef41Sopenharmony_ci  counter.dec();
471cb0ef41Sopenharmony_ci  assert.strictEqual(origin, 'unhandledRejection', err);
481cb0ef41Sopenharmony_ci  const knownError = errors.shift();
491cb0ef41Sopenharmony_ci  assert.deepStrictEqual(err, knownError);
501cb0ef41Sopenharmony_ci  // Check if the errors are reference equal.
511cb0ef41Sopenharmony_ci  assert(identical.shift() ? err === knownError : err !== knownError);
521cb0ef41Sopenharmony_ci}, 2));
53