Skip to content

Commit

Permalink
return 504, 502 and 500 as appropriate Fixes #131 (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert authored and chimurai committed Dec 7, 2016
1 parent 100681c commit 32b4c06
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
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

0 comments on commit 32b4c06

Please sign in to comment.