Skip to content

Commit

Permalink
events: provide better error message for unhandled error
Browse files Browse the repository at this point in the history
Previously, in the event of an unhandled error event, if the error is a
not an actual Error, then a default error is thrown. Now, the argument
is appended to the error message and added as the `context` property
of the error.

PR-URL: #1654
Reviewed-By: Rod Vagg <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
evanlucas committed May 12, 2015
1 parent 08d0866 commit 8b9a153
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ EventEmitter.prototype.emit = function emit(type) {
} else if (er instanceof Error) {
throw er; // Unhandled 'error' event
} else {
throw new Error('Uncaught, unspecified "error" event.');
// At least give some kind of context to the user
var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
err.context = er;
throw err;
}
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-event-emitter-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var EventEmitter = require('events');
var assert = require('assert');

var EE = new EventEmitter();

assert.throws(function() {
EE.emit('error', 'Accepts a string');
}, /Accepts a string/);

0 comments on commit 8b9a153

Please sign in to comment.