Permalink
Browse files

Allow the `errorHandler` property to be set on individual 'stack' ins…

…tances.

Previously. only 1 pre-defined error handler could be set on `Stack.errorHandler`.

This new behavior is backwards-compatible with the regular API, but you now have
the option of setting a custom `errorHandler` function on a per-'stack' basis.
  • Loading branch information...
1 parent 2f5005b commit 7fee4b4cb7f7a523b8de2d202e1750ae0fbc2c84 @TooTallNate committed Feb 16, 2011
Showing with 10 additions and 4 deletions.
  1. +10 −4 stack.js
View
@@ -1,19 +1,25 @@
function Stack(/*layers*/) {
- var error = Stack.errorHandler,
- handle = error;
+ function error() {
+ return handle.errorHandler;
+ }
+ var handle = function (req, res, next) {
+ return error().call(this, req, res, next);
+ }
+ handle.__proto__ = Stack;
Array.prototype.slice.call(arguments).reverse().forEach(function (layer) {
var child = handle;
handle = function (req, res) {
var self = this;
try {
layer.call(this, req, res, function (err) {
- if (err) { return error.call(self, req, res, err); }
+ if (err) { return error().call(self, req, res, err); }
child.call(self, req, res);
});
} catch (err) {
- error.call(this, req, res, err);
+ error().call(this, req, res, err);
}
};
+ handle.__proto__ = child;
});
return handle;
}

1 comment on commit 7fee4b4

I really want stack's layer delegation to be as few function calls as possible. This adds considerable function call overhead. If you want to add a lot of features to stack and don't need insane performance, then I recommend forking it and making a custom version. Since there is no bundled middleware it's very easy to replace my Stack with a custom stack and leave everything else the same.

Please sign in to comment.