Skip to content
Closed
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
11 changes: 10 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,18 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
// up in Node's output if this results in an unhandled exception.
throw er; // Unhandled 'error' event
}

let stringifiedEr;
const { inspect } = require('internal/util/inspect');
try {
stringifiedEr = inspect(er);
} catch {
stringifiedEr = er;
}

// At least give some kind of context to the user
const errors = lazyErrors();
const err = new errors.ERR_UNHANDLED_ERROR(er);
const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr);
err.context = er;
throw err; // Unhandled 'error' event
}
Expand Down
15 changes: 14 additions & 1 deletion test/parallel/test-event-emitter-errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const common = require('../common');
const EventEmitter = require('events');
const util = require('util');

const EE = new EventEmitter();

Expand All @@ -9,12 +10,24 @@ common.expectsError(
{
code: 'ERR_UNHANDLED_ERROR',
type: Error,
message: 'Unhandled error. (Accepts a string)'
message: "Unhandled error. ('Accepts a string')"
}
);

common.expectsError(
() => EE.emit('error', { message: 'Error!' }),
{
code: 'ERR_UNHANDLED_ERROR',
type: Error,
message: "Unhandled error. ({ message: 'Error!' })"
}
);

common.expectsError(
() => EE.emit('error', {
message: 'Error!',
[util.inspect.custom]() { throw new Error(); }
}),
{
code: 'ERR_UNHANDLED_ERROR',
type: Error,
Expand Down