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
fix(server): error in pre handler triggers after event #1500
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -753,17 +753,14 @@ Server.prototype._handle = function _handle(req, res) { | |
// run pre() handlers first before routing and running | ||
if (self.before.length > 0) { | ||
self._run(req, res, null, self.before, function (err) { | ||
// check for return false here - like with the regular handlers, | ||
// if false is returned we already sent a response and should stop | ||
// processing. | ||
if (err === false) { | ||
self._finishReqResCycle(req, res); | ||
// Like with regular handlers, if we are provided an error, we | ||
// should abort the middleware chain and fire after events. | ||
if (err !== undefined) { | ||
self._finishReqResCycle(req, res, null, err); | ||
return; | ||
} | ||
|
||
if (!err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come this got dropped? I think if you remove this there is the edge case where the "err" object is not false or a function? |
||
routeAndRun(); | ||
} | ||
routeAndRun(); | ||
}); | ||
} else { | ||
routeAndRun(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2957,3 +2957,22 @@ test('calling next twice should throw', function (t) { | |
t.ifError(err); | ||
}); | ||
}); | ||
|
||
test('aborting pre should still call after', function (t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put this at/around L2312 to group it with the other pre handler test that is testing |
||
setTimeout(function () { | ||
t.fail('Timed out'); | ||
t.end(); | ||
}, 2000); | ||
var error = new Error(); | ||
SERVER.pre(function (req, res, next) { | ||
next(error); | ||
}); | ||
SERVER.get('/', function (req, res, next) { | ||
t.fail('should have aborted stack before routing'); | ||
}); | ||
SERVER.on('after', function (req, res, route, err) { | ||
t.equal(err, error); | ||
t.end(); | ||
}); | ||
CLIENT.get('/', function () {}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior is slightly inconsistent compared to how its implemented in the
use
chain, where we are explicitly checking forfalse
orerr
. The result is that callingreturn next({})
could trigger an early exit in pre where it wouldn't in use. I don't mind revisiting what nexting with a POJO means, but probably worth tackling in a separate PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, was on the fence about this. The explicitness bit us in the butt here 😄 so I erred on the side of assuming everything was a cause to abort the chain. Though I guess if someone were to do
next(true)
ornext(null)
this would drop the chain which may not be desirable.