Skip to content

Commit

Permalink
Handle exceptions in filters
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Aug 4, 2013
1 parent 02782b9 commit 512890b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ function matcherIndex(arr, event) {
return -1;
}

function chain(filter, next) {
function chain(filter, then) {
return function (scope, re, callback) {
filter.call(scope, function (cb) {
next(scope, re, cb || callback);
}, callback);
function next(cb) {
then(scope, re, cb || callback);
}
try {
filter.call(scope, next, callback);
} catch (e) {
callback(e);
}
};
}

Expand Down
28 changes: 28 additions & 0 deletions test/test-addFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ test('hub.addFilter', {
this.hub.emit('test.b');

sinon.assert.calledTwice(spy);
},

'yields error if filter throws': function () {
var spy = sinon.spy();
var err = new Error('ouch');
this.hub.addFilter('test', function () {
throw err;
});

this.hub.emit('test', spy);

sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, err);
},

'does not invoke next filter or listeners if filter throws': function () {
var filter = sinon.spy(function (next) { next(); });
var listener = sinon.spy();
this.hub.addFilter('test', function () {
throw new Error('ouch');
});
this.hub.addFilter('test', filter);
this.hub.addListener('test', listener);

this.hub.emit('test', function () {});

sinon.assert.notCalled(filter);
sinon.assert.notCalled(listener);
}

});
Expand Down

0 comments on commit 512890b

Please sign in to comment.