diff --git a/lib/events.js b/lib/events.js index 6f279a0bff603c..ae847dc2341032 100644 --- a/lib/events.js +++ b/lib/events.js @@ -136,7 +136,13 @@ function identicalSequenceRange(a, b) { } function enhanceStackTrace(err, own) { - const sep = '\nEmitted \'error\' event at:\n'; + let ctorInfo = ''; + try { + const { name } = this.constructor; + if (name !== 'EventEmitter') + ctorInfo = ` on ${name} instance`; + } catch {} + const sep = `\nEmitted 'error' event${ctorInfo} at:\n`; const errStack = err.stack.split('\n').slice(1); const ownStack = own.stack.split('\n').slice(1); @@ -170,7 +176,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { // eslint-disable-next-line no-restricted-syntax Error.captureStackTrace(capture, EventEmitter.prototype.emit); Object.defineProperty(er, kEnhanceStackBeforeInspector, { - value: enhanceStackTrace.bind(null, er, capture), + value: enhanceStackTrace.bind(this, er, capture), configurable: true }); } catch {} diff --git a/test/message/events_unhandled_error_subclass.js b/test/message/events_unhandled_error_subclass.js new file mode 100644 index 00000000000000..8303cb2237dc1e --- /dev/null +++ b/test/message/events_unhandled_error_subclass.js @@ -0,0 +1,5 @@ +'use strict'; +require('../common'); +const EventEmitter = require('events'); +class Foo extends EventEmitter {} +new Foo().emit('error', new Error()); diff --git a/test/message/events_unhandled_error_subclass.out b/test/message/events_unhandled_error_subclass.out new file mode 100644 index 00000000000000..970533299ac60b --- /dev/null +++ b/test/message/events_unhandled_error_subclass.out @@ -0,0 +1,17 @@ +events.js:* + throw er; // Unhandled 'error' event + ^ + +Error + at Object. (*events_unhandled_error_subclass.js:*:*) + at Module._compile (internal/modules/cjs/loader.js:*:*) + at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) + at Module.load (internal/modules/cjs/loader.js:*:*) + at Function.Module._load (internal/modules/cjs/loader.js:*:*) + at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at internal/main/run_main_module.js:*:* +Emitted 'error' event on Foo instance at: + at Object. (*events_unhandled_error_subclass.js:*:*) + at Module._compile (internal/modules/cjs/loader.js:*:*) + [... lines matching original stack trace ...] + at internal/main/run_main_module.js:*:*