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 invalid triggers of the uncaughtException handler #1710
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 |
---|---|---|
|
@@ -816,7 +816,7 @@ Server.prototype._onRequest = function _onRequest(req, res) { | |
handlerDomain.add(req); | ||
handlerDomain.add(res); | ||
handlerDomain.on('error', function onError(err) { | ||
self._onHandlerError(err, req, res); | ||
self._onHandlerError(err, req, res, true); | ||
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. I think this would still make errors emitted on objects bound to the domain (in this case If we consider that is indeed not what the behavior of @mridgway @DonutEspresso Thoughts? 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. I believe that behavior is consistent with Restify versions <7. I'm not sure what the original intention was though. I agree that it would make more sense to filter to only uncaught exceptions. 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.
I just verified that looking at the 6.x branch of this repository and this is correct. Thus I'm fine with keeping that behavior, at least for now. |
||
}); | ||
handlerDomain.run(function run() { | ||
self._runPre(req, res); | ||
|
@@ -1065,9 +1065,15 @@ Server.prototype._onHandlerStop = function _onHandlerStop(req, res) { | |
* @param {Error|String|undefined} err - router handler error or route name | ||
* @param {Request} req - request | ||
* @param {Response} res - response | ||
* @param {boolean} isUncaught - whether the error is uncaught | ||
* @returns {undefined} no return value | ||
*/ | ||
Server.prototype._onHandlerError = function _onHandlerError(err, req, res) { | ||
Server.prototype._onHandlerError = function _onHandlerError( | ||
err, | ||
req, | ||
res, | ||
isUncaught | ||
) { | ||
var self = this; | ||
|
||
// Call route by name | ||
|
@@ -1098,7 +1104,7 @@ Server.prototype._onHandlerError = function _onHandlerError(err, req, res) { | |
res.err = res.err || err; | ||
|
||
// Error happened in router handlers | ||
self._routeErrorResponse(req, res, err); | ||
self._routeErrorResponse(req, res, err, isUncaught); | ||
}; | ||
|
||
/** | ||
|
@@ -1256,16 +1262,22 @@ Server.prototype._finishReqResCycle = function _finishReqResCycle( | |
* @param {Request} req - the request object | ||
* @param {Response} res - the response object | ||
* @param {Error} err - error | ||
* @param {boolean} isUncaught - whether the error is uncaught | ||
* @returns {undefined} no return value | ||
*/ | ||
Server.prototype._routeErrorResponse = function _routeErrorResponse( | ||
req, | ||
res, | ||
err | ||
err, | ||
isUncaught | ||
) { | ||
var self = this; | ||
|
||
if (self.listenerCount('uncaughtException') > 1) { | ||
if ( | ||
isUncaught && | ||
self.handleUncaughtExceptions && | ||
self.listenerCount('uncaughtException') > 1 | ||
) { | ||
self.emit('uncaughtException', req, res, req.route, err); | ||
return; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -674,6 +674,42 @@ test('GH-77 uncaughtException (default behavior)', function(t) { | |
}); | ||
}); | ||
|
||
// eslint-disable-next-line | ||
test('handleUncaughtExceptions should not call handler for internal errors', function(t) { | ||
SERVER.get('/', function(req, res, next) { | ||
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. I'm a little bit confused about this test setup. I think we trying to verify restify/router errors (e.g., 404) don't trigger uncaught exceptions? If yes, should we just remove the get route and have the client do a get against a non-existent route? 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. There seems to be a behavior difference if you have no routes (which 404s) and having only GET routes and trying HEAD (results in MethodNotAllowed error). I agree that this makes the test confusing, but I think if the behavior difference should be fixed, it should be fixed separately. 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. Gotcha - thanks for context! I expect this is related to CORS use cases - do you mind adding all of this as a comment to the test? Otherwise, it's a bit confusing to register the empty 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. Makes sense. I added a comment and the assertion that you mentioned. |
||
res.send('Hello'); | ||
}); | ||
|
||
SERVER.on('uncaughtException', function throwError(err) { | ||
t.ifError(err); | ||
t.end(); | ||
}); | ||
|
||
CLIENT.head('/', function(err, _, res) { | ||
t.ok(err); | ||
t.equal(res.statusCode, 405); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
// eslint-disable-next-line | ||
test('handleUncaughtExceptions should not call handler for next(new Error())', 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. 👍 |
||
SERVER.get('/', function(req, res, next) { | ||
next(new Error('I am not fatal')); | ||
}); | ||
|
||
SERVER.on('uncaughtException', function throwError(err) { | ||
t.ifError(err); | ||
t.end(); | ||
}); | ||
|
||
CLIENT.get('/', function(err, _, res) { | ||
t.ok(err); | ||
t.equal(res.statusCode, 500); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('GH-77 uncaughtException (with custom handler)', function(t) { | ||
SERVER.on('uncaughtException', function(req, res, route, err) { | ||
res.send(204); | ||
|
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.
Isn't this already gated (
opts.handleUncaughtExceptions
) on L84?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.
Yeah, it is indeed. I'll remove this check.