Permalink
Browse files

Handle should accept callback

  • Loading branch information...
scttnlsn committed Sep 10, 2012
1 parent 227c582 commit 0107008fe2e4146d9995105e913e440ada854457
Showing with 12 additions and 7 deletions.
  1. +8 −4 pagoda.js
  2. +4 −3 test/test_pagoda.js
View
@@ -35,22 +35,26 @@
return this;
};
- Stack.prototype.handle = function(context) {
+ Stack.prototype.handle = function(context, callback) {
var self = this;
var index = 0;
+ if (typeof context === 'function' && callback === undefined) {
+ callback = context;
+ context = {};
+ }
+
function next(err) {
var layer = self.layers[index++];
if (!layer) {
// Reached bottom of stack
- if (err) throw err;
- return;
+ return callback(err);
}
var exec = function(args) {
try {
- layer.apply(context || {}, args);
+ layer.apply(context, args);
} catch (err) {
next(err);
}
View
@@ -64,13 +64,14 @@ describe('Stack', function() {
stack.handle();
});
- it('throws unhandled errors', function() {
+ it('passes unhandled errors to handle callback', function(done) {
stack.use(function() {
throw new Error();
});
- assert.throws(function() {
- stack.handle();
+ stack.handle(function(err) {
+ assert.ok(err);
+ done();
});
});

0 comments on commit 0107008

Please sign in to comment.