From 63edde0e01e577691002c2f2e39fb30abccec1bc Mon Sep 17 00:00:00 2001 From: isaacs Date: Sat, 2 Mar 2013 15:11:23 -0800 Subject: [PATCH] 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. --- lib/events.js | 6 +++--- test/simple/test-event-emitter-subclass.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/events.js b/lib/events.js index dd4fdf64e7..811cb90895 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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') diff --git a/test/simple/test-event-emitter-subclass.js b/test/simple/test-event-emitter-subclass.js index 537b682088..7a2f778e22 100644 --- a/test/simple/test-event-emitter-subclass.js +++ b/test/simple/test-event-emitter-subclass.js @@ -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');