• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { execFile } = require('child_process');
6const fixtures = require('../common/fixtures');
7
8{
9  // Verify exit behavior is unchanged
10  const fixture = fixtures.path('uncaught-exceptions', 'uncaught-monitor1.js');
11  execFile(
12    process.execPath,
13    [fixture],
14    common.mustCall((err, stdout, stderr) => {
15      assert.strictEqual(err.code, 1);
16      assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
17      assert.strictEqual(stdout, 'Monitored: Shall exit\n');
18      const errLines = stderr.trim().split(/[\r\n]+/);
19      const errLine = errLines.find((l) => /^Error/.exec(l));
20      assert.strictEqual(errLine, 'Error: Shall exit');
21    })
22  );
23}
24
25{
26  // Verify exit behavior is unchanged
27  const fixture = fixtures.path('uncaught-exceptions', 'uncaught-monitor2.js');
28  execFile(
29    process.execPath,
30    [fixture],
31    common.mustCall((err, stdout, stderr) => {
32      assert.strictEqual(err.code, 7);
33      assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
34      assert.strictEqual(stdout, 'Monitored: Shall exit, will throw now\n');
35      const errLines = stderr.trim().split(/[\r\n]+/);
36      const errLine = errLines.find((l) => /^ReferenceError/.exec(l));
37      assert.strictEqual(
38        errLine,
39        'ReferenceError: missingFunction is not defined'
40      );
41    })
42  );
43}
44
45const theErr = new Error('MyError');
46
47process.on(
48  'uncaughtExceptionMonitor',
49  common.mustCall((err, origin) => {
50    assert.strictEqual(err, theErr);
51    assert.strictEqual(origin, 'uncaughtException');
52  }, 2)
53);
54
55process.on('uncaughtException', common.mustCall((err, origin) => {
56  assert.strictEqual(origin, 'uncaughtException');
57  assert.strictEqual(err, theErr);
58}));
59
60process.nextTick(common.mustCall(() => {
61  // Test with uncaughtExceptionCaptureCallback installed
62  process.setUncaughtExceptionCaptureCallback(common.mustCall(
63    (err) => assert.strictEqual(err, theErr))
64  );
65
66  throw theErr;
67}));
68
69throw theErr;
70