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

return 504, 502 and 500 as appropriate Fixes #131 #132

Merged
merged 1 commit into from
Dec 7, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,21 @@ function getProxyEventHandlers(opts) {

function defaultErrorHandler(err, req, res) {
var host = (req.headers && req.headers.host);
var code = err.code;

if (res.writeHead && !res.headersSent) {
res.writeHead(500);
if (/HPE_INVALID/.test(code)) {
res.writeHead(502);
} else {
switch(code) {
case 'ECONNRESET':
case 'ENOTFOUND':
case 'ECONNREFUSED':
res.writeHead(504);
break;
default: res.writeHead(500);
}
}
}

res.end('Error occured while trying to proxy to: ' + host + req.url);
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/http-proxy-middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('E2E http-proxy-middleware', function() {
});

it('should handle errors when host is not reachable', function() {
expect(response.statusCode).to.equal(500);
expect(response.statusCode).to.equal(504);
});
});

Expand Down
18 changes: 15 additions & 3 deletions test/unit/handlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,21 @@ describe('default proxy error handler', function() {
errorMessage = undefined;
});

it('should set the http status code to: 500', function() {
proxyError(mockError, mockReq, mockRes, proxyOptions);
expect(httpErrorCode).to.equal(500);
var codes = [
['HPE_INVALID_FOO', 502],
['HPE_INVALID_BAR', 502],
['ECONNREFUSED', 504],
['ENOTFOUND', 504],
['ECONNREFUSED', 504],
['any', 500],
];
codes.forEach(function(item) {
var msg = item[0];
var code = item[1];
it('should set the http status code for ' + msg + ' to: ' + code, function() {
proxyError({ code: msg }, mockReq, mockRes, proxyOptions);
expect(httpErrorCode).to.equal(code);
});
});

it('should end the response and return error message', function() {
Expand Down