Skip to content

Commit 8b9a153

Browse files
committed
events: provide better error message for unhandled error
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 <rod@vagg.org> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 08d0866 commit 8b9a153

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/events.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ EventEmitter.prototype.emit = function emit(type) {
140140
} else if (er instanceof Error) {
141141
throw er; // Unhandled 'error' event
142142
} else {
143-
throw new Error('Uncaught, unspecified "error" event.');
143+
// At least give some kind of context to the user
144+
var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
145+
err.context = er;
146+
throw err;
144147
}
145148
return false;
146149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var EventEmitter = require('events');
2+
var assert = require('assert');
3+
4+
var EE = new EventEmitter();
5+
6+
assert.throws(function() {
7+
EE.emit('error', 'Accepts a string');
8+
}, /Accepts a string/);

0 commit comments

Comments
 (0)