summaryrefslogtreecommitdiff
path: root/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js
blob: a2afebd838f4109b6e119980e6b8d861821a9f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';

// This test makes sure that when throwing an error from a domain, and then
// handling that error in an uncaughtException handler by throwing an error
// again, the exit code, signal and error messages are the ones we expect with
// and without using --abort-on-uncaught-exception.

const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const domain = require('domain');

const uncaughtExceptionHandlerErrMsg = 'boom from uncaughtException handler';
const domainErrMsg = 'boom from domain';

const RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE = 42;

if (process.argv[2] === 'child') {
  process.on('uncaughtException', common.mustCall(function onUncaught() {
    if (process.execArgv.includes('--abort-on-uncaught-exception')) {
      // When passing --abort-on-uncaught-exception to the child process,
      // we want to make sure that this handler (the process' uncaughtException
      // event handler) wasn't called. Unfortunately we can't parse the child
      // process' output to do that, since on Windows the standard error output
      // is not properly flushed in V8's Isolate::Throw right before the
      // process aborts due to an uncaught exception, and thus the error
      // message representing the error that was thrown cannot be read by the
      // parent process. So instead of parsing the child process' standard
      // error, the parent process will check that in the case
      // --abort-on-uncaught-exception was passed, the process did not exit
      // with exit code RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE.
      process.exit(RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE);
    } else {
      // On the other hand, when not passing --abort-on-uncaught-exception to
      // the node process, we want to throw in this event handler to make sure
      // that the proper error message, exit code and signal are the ones we
      // expect.
      throw new Error(uncaughtExceptionHandlerErrMsg);
    }
  }));

  const d = domain.create();
  d.run(common.mustCall(function() {
    throw new Error(domainErrMsg);
  }));
} else {
  runTestWithoutAbortOnUncaughtException();
  runTestWithAbortOnUncaughtException();
}

function runTestWithoutAbortOnUncaughtException() {
  child_process.exec(
    createTestCmdLine(),
    function onTestDone(err, stdout, stderr) {
      // When _not_ passing --abort-on-uncaught-exception, the process'
      // uncaughtException handler _must_ be called, and thus the error
      // message must include only the message of the error thrown from the
      // process' uncaughtException handler.
      assert(stderr.includes(uncaughtExceptionHandlerErrMsg),
             'stderr output must include proper uncaughtException ' +
             'handler\'s error\'s message');
      assert(!stderr.includes(domainErrMsg),
             'stderr output must not include domain\'s error\'s message');

      assert.notStrictEqual(err.code, 0,
                            'child process should have exited with a ' +
                            'non-zero exit code, but did not');
    }
  );
}

function runTestWithAbortOnUncaughtException() {
  child_process.exec(createTestCmdLine({
    withAbortOnUncaughtException: true
  }), function onTestDone(err, stdout, stderr) {
    assert.notStrictEqual(err.code, RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE,
                          'child process should not have run its ' +
                          'uncaughtException event handler');
    assert(common.nodeProcessAborted(err.code, err.signal),
           'process should have aborted, but did not');
  });
}

function createTestCmdLine(options) {
  let testCmd = '';

  if (!common.isWindows) {
    // Do not create core files, as it can take a lot of disk space on
    // continuous testing and developers' machines
    testCmd += 'ulimit -c 0 && ';
  }

  testCmd += `"${process.argv[0]}"`;

  if (options && options.withAbortOnUncaughtException) {
    testCmd += ' --abort-on-uncaught-exception';
  }

  testCmd += ` "${process.argv[1]}" child`;

  return testCmd;
}