Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: allow monitoring uncaughtException #31257

Closed
7 changes: 7 additions & 0 deletions test/fixtures/uncaught-exceptions/uncaught-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

process.on(process.uncaughtExceptionMonitor, (err) => {
console.log(`Monitored: ${err.message}`);
Flarna marked this conversation as resolved.
Show resolved Hide resolved
});

Flarna marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Shall exit');
21 changes: 20 additions & 1 deletion test/parallel/test-process-uncaught-exception-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

const common = require('../common');
const assert = require('assert');
const { execFile } = require('child_process');
const fixtures = require('../common/fixtures');

{
// Verify exit behavior is unchanged
const fixture = fixtures.path('uncaught-exceptions', 'uncaught-monitor.js');
execFile(
process.execPath,
[fixture],
common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.code, 1);
assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
assert.strictEqual(stdout, 'Monitored: Shall exit\n');
const errLines = stderr.trim().split(/[\r\n]+/);
const errLine = errLines.find((l) => /^Error/.exec(l));
assert.strictEqual(errLine, 'Error: Shall exit');
})
);
}

const theErr = new Error('MyError');

Expand All @@ -18,8 +37,8 @@ process.on('uncaughtException', common.mustCall((err, origin) => {
assert.strictEqual(err, theErr);
}));

// Test with uncaughtExceptionCaptureCallback installed
process.nextTick(common.mustCall(() => {
// Test with uncaughtExceptionCaptureCallback installed
process.setUncaughtExceptionCaptureCallback(common.mustCall(
(err) => assert.strictEqual(err, theErr))
);
Expand Down