-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Conversation
lib/events.js
Outdated
@@ -160,7 +160,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 + ')'); | |||
var err = new Error('An "error" event was emitted, but no listeners are attached to this emitter: (' + er + ')'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too long.
lib/events.js
Outdated
@@ -151,7 +151,7 @@ EventEmitter.prototype.emit = function emit(type) { | |||
er = arguments[1]; | |||
if (domain) { | |||
if (!er) | |||
er = new Error('Uncaught, unspecified "error" event'); | |||
er = new Error('An "error" event was emitted, but no listeners are attached to this emitter.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unspecified is confusing, yes, but I don't think the text is an improvement. I can think of 2 other ways to avoid the abort that don't involve listening for the error (the 'uncaughtException' handler and domains), so the text is both too long, and not correct.
How about just 'Uncaught "error" event'
?
Fixed, sorry, didn't realize that running tests didn't run the linter as well. |
Sure, that's reasonable. Unspecified was what really threw me off. I'll update and rebase. |
Also, needs a unit test - and yes, I know, it should have already had one, sorry! |
Seemed like a message was the cleanest way to add a test for this. If that's not the right approach, let me know and I can update! |
throw err; | ||
^ | ||
|
||
Error: Uncaught "error" event: ([object Object]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[object Object]
is probably not the desired output.
You don't need a message test. There is almost certainly a test that already exercises this behavior. You would just need to add an assertion on the error message in such a test. |
OK, found the real test and just updated the regex to include the changes. |
lib/events.js
Outdated
@@ -160,7 +160,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 + ')'); | |||
var err = new Error('Uncaught "error" event: (' + er + ')'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either the parens should be dropped or the colon removed.
IMHO I think "uncaught" is a bit of a misnomer, since to me it sounds like the error was thrown because of a missing try-catch or something. What about "unhandled" instead? |
Yeah, unhandled is more accurate than uncaught. Will push an update in a minute that changes it to that. |
@@ -7,4 +7,4 @@ var EE = new EventEmitter(); | |||
|
|||
assert.throws(function() { | |||
EE.emit('error', 'Accepts a string'); | |||
}, /Accepts a string/); | |||
}, /Unhandled "error" event. \(Accepts a string\)/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People should not be emitting strings or other things, even though we technically support it. So, in most cases, we'll print the rather unhelpful [object Object]
, like in your previous test, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly in all of my team's code, we pass an object to the emitter so it would just spit that out, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd opt to just not try to print the error. You could use util.format()
, but it prints the whole stack trace for errors, so it's kind of loud.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just omitted it and made the test reflect a real world use case slightly better.
@Trott: I think maybe I was just on the wrong branch. It was my first time pulling down and running the node tests. I tried to recreate it to see if it was something with first time configuring & running tests, and it ran the linter. |
@@ -7,4 +7,8 @@ var EE = new EventEmitter(); | |||
|
|||
assert.throws(function() { | |||
EE.emit('error', 'Accepts a string'); | |||
}, /Accepts a string/); | |||
}, /Unhandled "error" event/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: how about using ^Error: Unhandled "error" event$
? Pointing this out only because other tests do this.
Re-run of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM but might have broader impact than expected. @nodejs/ctc thoughts?
@@ -160,7 +160,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 + ')'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
There was a problem hiding this comment.
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
lib/events.js
Outdated
@@ -160,7 +160,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 + ')'); | |||
var err = new Error('Unhandled "error" event'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Prefer const
over var
wherever possible.
@nodejs/ctc ... any further thoughts on this one? |
Update error message that's thrown when no error listeners are attached to an emitter. This would have been trivial to debug if I had understood what 'unspecified error' was supposed to mean. Once I went looking at the source, and saw the comment on 149, it was easy to fix.
Rebased, and a few minor things based on the other feedback (adding er back, changing var to const, using fat arrow functions in the test instead.) |
@sam-github @cjihrig ... PTAL |
Update error message that's thrown when no error listeners are attached to an emitter. PR-URL: #10387 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Italo A. Casas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
Landed in 2141d37 |
eventemitter: clarify error message
Update error message that's thrown when no error listeners are attached to an emitter. This would have been trivial to debug if I had understood what 'unspecified error' was supposed to mean. Once I went looking at the source, and saw the comment on 149, it was easy to fix.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
eventemitter
Description of change
Update error message that's thrown when no error listeners are attached to an emitter. This would have been trivial to debug if I had understood what 'unspecified error' was supposed to mean. Once I went looking at the source, and saw the comment on 149, it was easy to fix.