Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
},
fatalExceptionStackEnhancers,
} = require('internal/errors');
const { validateFunction } = require('internal/validators');
const { pathToFileURL } = require('internal/url');
Expand Down Expand Up @@ -174,6 +175,10 @@ function createOnGlobalUncaughtException() {
// call that threw and was never cleared. So clear it now.
clearDefaultTriggerAsyncId();

if (er != null && typeof er === 'object') {
fatalExceptionStackEnhancers.beforeInspector(er);
}

const type = fromPromise ? 'unhandledRejection' : 'uncaughtException';
process.emit('uncaughtExceptionMonitor', er, type);
// Primary callback (e.g., domain) has priority and always handles the exception
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-process-uncaught-exception-enhanced-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
const common = require('../common');
const assert = require('node:assert');
const EventEmitter = require('node:events');

class CustomEmitter extends EventEmitter {}

const ee = new EventEmitter();
const customEE = new CustomEmitter();

let monitorCount = 0;
let uncaughtCount = 0;

process.on('uncaughtExceptionMonitor', common.mustCall((err, origin) => {
assert.strictEqual(origin, 'uncaughtException');
monitorCount++;
if (monitorCount === 1) {
assert.match(err.stack, /Emitted 'error' event at:/);
assert.match(err.stack, /at emitPlainError/);
} else if (monitorCount === 2) {
assert.match(err.stack, /Emitted 'error' event on CustomEmitter instance at:/);
assert.match(err.stack, /at emitCustomClassError/);
}
}, 2));

process.on('uncaughtException', common.mustCall((err, origin) => {
assert.strictEqual(origin, 'uncaughtException');
uncaughtCount++;
if (uncaughtCount === 1) {
assert.match(err.stack, /Emitted 'error' event at:/);
assert.match(err.stack, /at emitPlainError/);
process.nextTick(emitCustomClassError);
} else if (uncaughtCount === 2) {
assert.match(err.stack, /Emitted 'error' event on CustomEmitter instance at:/);
assert.match(err.stack, /at emitCustomClassError/);
}
}, 2));

function emitPlainError() {
ee.emit('error', new Error('plain error'));
}

function emitCustomClassError() {
customEE.emit('error', new Error('custom class error'));
}

emitPlainError();
Loading