11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { execFile } = require('child_process');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  // Verify exit behavior is unchanged
101cb0ef41Sopenharmony_ci  const fixture = fixtures.path('uncaught-exceptions', 'uncaught-monitor1.js');
111cb0ef41Sopenharmony_ci  execFile(
121cb0ef41Sopenharmony_ci    process.execPath,
131cb0ef41Sopenharmony_ci    [fixture],
141cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
151cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 1);
161cb0ef41Sopenharmony_ci      assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
171cb0ef41Sopenharmony_ci      assert.strictEqual(stdout, 'Monitored: Shall exit\n');
181cb0ef41Sopenharmony_ci      const errLines = stderr.trim().split(/[\r\n]+/);
191cb0ef41Sopenharmony_ci      const errLine = errLines.find((l) => /^Error/.exec(l));
201cb0ef41Sopenharmony_ci      assert.strictEqual(errLine, 'Error: Shall exit');
211cb0ef41Sopenharmony_ci    })
221cb0ef41Sopenharmony_ci  );
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  // Verify exit behavior is unchanged
271cb0ef41Sopenharmony_ci  const fixture = fixtures.path('uncaught-exceptions', 'uncaught-monitor2.js');
281cb0ef41Sopenharmony_ci  execFile(
291cb0ef41Sopenharmony_ci    process.execPath,
301cb0ef41Sopenharmony_ci    [fixture],
311cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
321cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 7);
331cb0ef41Sopenharmony_ci      assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
341cb0ef41Sopenharmony_ci      assert.strictEqual(stdout, 'Monitored: Shall exit, will throw now\n');
351cb0ef41Sopenharmony_ci      const errLines = stderr.trim().split(/[\r\n]+/);
361cb0ef41Sopenharmony_ci      const errLine = errLines.find((l) => /^ReferenceError/.exec(l));
371cb0ef41Sopenharmony_ci      assert.strictEqual(
381cb0ef41Sopenharmony_ci        errLine,
391cb0ef41Sopenharmony_ci        'ReferenceError: missingFunction is not defined'
401cb0ef41Sopenharmony_ci      );
411cb0ef41Sopenharmony_ci    })
421cb0ef41Sopenharmony_ci  );
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst theErr = new Error('MyError');
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciprocess.on(
481cb0ef41Sopenharmony_ci  'uncaughtExceptionMonitor',
491cb0ef41Sopenharmony_ci  common.mustCall((err, origin) => {
501cb0ef41Sopenharmony_ci    assert.strictEqual(err, theErr);
511cb0ef41Sopenharmony_ci    assert.strictEqual(origin, 'uncaughtException');
521cb0ef41Sopenharmony_ci  }, 2)
531cb0ef41Sopenharmony_ci);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciprocess.on('uncaughtException', common.mustCall((err, origin) => {
561cb0ef41Sopenharmony_ci  assert.strictEqual(origin, 'uncaughtException');
571cb0ef41Sopenharmony_ci  assert.strictEqual(err, theErr);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  process.nextTick(common.mustCall(() => {
601cb0ef41Sopenharmony_ci    // Test with uncaughtExceptionCaptureCallback installed
611cb0ef41Sopenharmony_ci    process.setUncaughtExceptionCaptureCallback(common.mustCall(
621cb0ef41Sopenharmony_ci      (err) => assert.strictEqual(err, theErr))
631cb0ef41Sopenharmony_ci    );
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    throw theErr;
661cb0ef41Sopenharmony_ci  }));
671cb0ef41Sopenharmony_ci}));
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cithrow theErr;
70