events: Handle emit('error') before ctor

The previous commit did not handle the case when the event type
is 'error', since that is checked before reading the handler.
This commit is contained in:
isaacs 2013-03-02 15:11:23 -08:00
parent d345c1173a
commit 63edde0e01
2 changed files with 13 additions and 3 deletions

View File

@ -53,6 +53,9 @@ EventEmitter.prototype.setMaxListeners = function(n) {
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
@ -73,9 +76,6 @@ EventEmitter.prototype.emit = function(type) {
}
}
if (!this._events)
this._events = {};
handler = this._events[type];
if (typeof handler === 'undefined')

View File

@ -38,6 +38,16 @@ var myee = new MyEE(function() {
called = true;
});
util.inherits(ErrorEE, EventEmitter);
function ErrorEE() {
this.emit('error', new Error('blerg'));
}
assert.throws(function() {
new ErrorEE();
}, /blerg/);
process.on('exit', function() {
assert(called);
console.log('ok');