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

events: update and clarify error message #10387

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ EventEmitter.prototype.emit = function emit(type) {
er = arguments[1];
if (domain) {
if (!er)
er = new Error('Uncaught, unspecified "error" event');
er = new Error('Unhandled "error" event');
if (typeof er === 'object' && er !== null) {
er.domainEmitter = this;
er.domain = domain;
Expand All @@ -188,7 +188,7 @@ EventEmitter.prototype.emit = function emit(type) {
throw er; // Unhandled 'error' event
} else {
// At least give some kind of context to the user
var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
const err = new Error('Unhandled "error" event. (' + er + ')');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's a good idea to remove the last part. I understand that if you emit a plain object, it will print [object Object] but it seems logical for the "error" event to emit an Error and in this case it would print the error message correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I would prefer that we keep the er in the message

err.context = er;
throw err;
}
Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-event-emitter-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const assert = require('assert');

const EE = new EventEmitter();

assert.throws(function() {
assert.throws(() => {
EE.emit('error', 'Accepts a string');
}, /Accepts a string/);
}, /^Error: Unhandled "error" event\. \(Accepts a string\)$/);

assert.throws(() => {
EE.emit('error', {message: 'Error!'});
}, /^Error: Unhandled "error" event\. \(\[object Object\]\)$/);