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

Conversation

ctide
Copy link
Contributor

@ctide ctide commented Dec 21, 2016

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), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected 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.

@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to the events subsystem / EventEmitter. lts-watch-v6.x labels Dec 21, 2016
@cjihrig cjihrig added the semver-major PRs that contain breaking changes and should be released in the next major version. label Dec 21, 2016
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 + ')');
Copy link
Contributor

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.');
Copy link
Contributor

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.

Copy link
Contributor

@sam-github sam-github left a 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'?

@ctide
Copy link
Contributor Author

ctide commented Dec 21, 2016

Fixed, sorry, didn't realize that running tests didn't run the linter as well.

@ctide
Copy link
Contributor Author

ctide commented Dec 21, 2016

Sure, that's reasonable. Unspecified was what really threw me off. I'll update and rebase.

@sam-github
Copy link
Contributor

Also, needs a unit test - and yes, I know, it should have already had one, sorry!

@ctide
Copy link
Contributor Author

ctide commented Dec 21, 2016

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])
Copy link
Contributor

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.

@cjihrig
Copy link
Contributor

cjihrig commented Dec 21, 2016

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.

@ctide
Copy link
Contributor Author

ctide commented Dec 21, 2016

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 + ')');
Copy link
Contributor

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.

@mscdex
Copy link
Contributor

mscdex commented Dec 21, 2016

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?

@ctide
Copy link
Contributor Author

ctide commented Dec 21, 2016

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\)/);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@ctide
Copy link
Contributor Author

ctide commented Dec 22, 2016

@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/);
Copy link
Member

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.

@italoacasas
Copy link
Contributor

@joaocgreis
Copy link
Member

Re-run of arm-fanned because of the RPi1 failures: https://ci.nodejs.org/job/node-test-commit-arm-fanned/6082/

Copy link
Member

@jasnell jasnell left a 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 + ')');
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

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');
Copy link
Contributor

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.

@jasnell
Copy link
Member

jasnell commented Mar 22, 2017

@nodejs/ctc ... any further thoughts on this one?
@ctide ... if you'd like to pursue this can you please give it a quick rebase?

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.
@ctide
Copy link
Contributor Author

ctide commented Mar 23, 2017

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.)

@jasnell
Copy link
Member

jasnell commented Mar 23, 2017

@sam-github @cjihrig ... PTAL

@jasnell
Copy link
Member

jasnell commented Mar 24, 2017

jasnell pushed a commit that referenced this pull request Mar 24, 2017
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]>
@jasnell
Copy link
Member

jasnell commented Mar 24, 2017

Landed in 2141d37

@jasnell jasnell changed the title Update events.js events: update and clarify error message Mar 24, 2017
@jasnell jasnell closed this Mar 24, 2017
@jasnell jasnell mentioned this pull request Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.