Skip to content
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

server _routeErrorResponse avoiding uncaughtException by only sending response when not sent #1568

Merged
merged 5 commits into from Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/server.js
Expand Up @@ -890,7 +890,9 @@ Server.prototype._routeErrorResponse = function _routeErrorResponse(
null,
err,
function _emitErrorEvents() {
res.send(err);
if (!res.headersSent) {
res.send(err);
}
return self._finishReqResCycle(req, res, null, err);
}
);
Expand Down
25 changes: 25 additions & 0 deletions test/server.test.js
Expand Up @@ -224,6 +224,31 @@ test('rm route and clear cached route', function(t) {
});
});

test('_routeErrorResponse does not cause uncaughtException when called when header has already been sent', function(
t
) {
SERVER.on('MethodNotAllowed', function(req, res, error, next) {
res.json(405, { status: 'MethodNotAllowed' });
try {
next();
} catch (err) {
t.fail(
'next() should not throw error when header has already been sent'
);
}
t.end();
});

SERVER.post('/routePostOnly', function tester(req, res, next) {
next();
});

CLIENT.get('/routePostOnly', function(err, _, res) {
t.ok(err);
t.equal(res.statusCode, 405);
});
});

test('GH-1171: rm one version of the routes, other versions should still work', function(
t
) {
Expand Down