New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

directly invoke error middleware #1085

Closed
wants to merge 5 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+17 −2
Diff settings

Always

Just for now

View
@@ -53,6 +53,8 @@ function createServer() {
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
// store for error middleware
app.errware = [];
return app;
}
@@ -104,7 +106,11 @@ proto.use = function use(route, fn) {
// add the middleware
debug('use %s %s', path || '/', handle.name || 'anonymous');
this.stack.push({ route: path, handle: handle });
if (handle.length === 4) {
this.errware.push({ route: path, handle: handle, index: this.stack.length });
} else {
this.stack.push({ route: path, handle: handle, index: this.errware.length });
}
return this;
};
@@ -118,10 +124,12 @@ proto.use = function use(route, fn) {
proto.handle = function handle(req, res, out) {
var index = 0;
var errIndex = 0;
var protohost = getProtohost(req.url) || '';
var removed = '';
var slashAdded = false;
var stack = this.stack;
var errware = this.errware;
// final function handler
var done = out || finalhandler(req, res, {
@@ -144,7 +152,14 @@ proto.handle = function handle(req, res, out) {
}
// next callback
var layer = stack[index++];
var layer;
if (err) {
layer = errware[errIndex++];
if (layer) index = layer.index;
} else {
layer = stack[index++];
if (layer) errIndex = layer.index;
}
// all done
if (!layer) {
ProTip! Use n and p to navigate between commits in a pull request.