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