11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// The goal of this test is to make sure that when a top-level error
41cb0ef41Sopenharmony_ci// handler throws an error following the handling of a previous error,
51cb0ef41Sopenharmony_ci// the process reports the error message from the error thrown in the
61cb0ef41Sopenharmony_ci// top-level error handler, not the one from the previous error.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cirequire('../common');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst domainErrHandlerExMessage = 'exception from domain error handler';
111cb0ef41Sopenharmony_ciconst internalExMessage = 'You should NOT see me';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
141cb0ef41Sopenharmony_ci  const domain = require('domain');
151cb0ef41Sopenharmony_ci  const d = domain.create();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  d.on('error', function() {
181cb0ef41Sopenharmony_ci    throw new Error(domainErrHandlerExMessage);
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  d.run(function doStuff() {
221cb0ef41Sopenharmony_ci    process.nextTick(function() {
231cb0ef41Sopenharmony_ci      throw new Error(internalExMessage);
241cb0ef41Sopenharmony_ci    });
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci} else {
271cb0ef41Sopenharmony_ci  const fork = require('child_process').fork;
281cb0ef41Sopenharmony_ci  const assert = require('assert');
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const child = fork(process.argv[1], ['child'], { silent: true });
311cb0ef41Sopenharmony_ci  let stderrOutput = '';
321cb0ef41Sopenharmony_ci  if (child) {
331cb0ef41Sopenharmony_ci    child.stderr.on('data', function onStderrData(data) {
341cb0ef41Sopenharmony_ci      stderrOutput += data.toString();
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    child.on('close', function onChildClosed() {
381cb0ef41Sopenharmony_ci      assert(stderrOutput.includes(domainErrHandlerExMessage));
391cb0ef41Sopenharmony_ci      assert.strictEqual(stderrOutput.includes(internalExMessage), false);
401cb0ef41Sopenharmony_ci    });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    child.on('exit', function onChildExited(exitCode, signal) {
431cb0ef41Sopenharmony_ci      const expectedExitCode = 7;
441cb0ef41Sopenharmony_ci      const expectedSignal = null;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci      assert.strictEqual(exitCode, expectedExitCode);
471cb0ef41Sopenharmony_ci      assert.strictEqual(signal, expectedSignal);
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
51