diff --git a/lib/events.js b/lib/events.js index cfceb5b040a486..0a220b5d821e7f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -124,22 +124,13 @@ function longestSeqContainedIn(a, b) { return [ 0, 0, 0 ]; } -function longestCommonSubsequence(a, b) { - const [ l1, i1, j1 ] = longestSeqContainedIn(a, b); - const [ l2, i2, j2 ] = longestSeqContainedIn(b, a); - if (l1 > l2) - return [ l1, j1, i1 ]; - else - return [ l2, i2, j2 ]; -} - function enhanceStackTrace(err, own) { const sep = '\nEmitted \'error\' event at:\n'; const errStack = err.stack.split('\n').slice(1); const ownStack = own.stack.split('\n').slice(1); - const [ len, off ] = longestCommonSubsequence(errStack, ownStack); + const [ len, off ] = longestSeqContainedIn(ownStack, errStack); if (len > 0) { ownStack.splice(off + 1, len - 1, ' [... lines matching original stack trace ...]'); diff --git a/test/parallel/test-events-uncaught-exception-stack.js b/test/parallel/test-events-uncaught-exception-stack.js new file mode 100644 index 00000000000000..c55322a5aa56c4 --- /dev/null +++ b/test/parallel/test-events-uncaught-exception-stack.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const EventEmitter = require('events'); + +// Tests that the error stack where the exception was thrown is *not* appended. + +process.on('uncaughtException', common.mustCall((err) => { + const lines = err.stack.split('\n'); + assert.strictEqual(lines[0], 'Error'); + lines.slice(1).forEach((line) => { + assert(/^ at/.test(line), `${line} has an unexpected format`); + }); +})); + +new EventEmitter().emit('error', new Error());