Skip to content

Commit 32b4c06

Browse files
graingertchimurai
authored andcommitted
return 504, 502 and 500 as appropriate Fixes chimurai#131 (chimurai#132)
1 parent 100681c commit 32b4c06

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

lib/handlers.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,21 @@ function getProxyEventHandlers(opts) {
4848

4949
function defaultErrorHandler(err, req, res) {
5050
var host = (req.headers && req.headers.host);
51+
var code = err.code;
5152

5253
if (res.writeHead && !res.headersSent) {
53-
res.writeHead(500);
54+
if (/HPE_INVALID/.test(code)) {
55+
res.writeHead(502);
56+
} else {
57+
switch(code) {
58+
case 'ECONNRESET':
59+
case 'ENOTFOUND':
60+
case 'ECONNREFUSED':
61+
res.writeHead(504);
62+
break;
63+
default: res.writeHead(500);
64+
}
65+
}
5466
}
5567

5668
res.end('Error occured while trying to proxy to: ' + host + req.url);

test/e2e/http-proxy-middleware.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ describe('E2E http-proxy-middleware', function() {
395395
});
396396

397397
it('should handle errors when host is not reachable', function() {
398-
expect(response.statusCode).to.equal(500);
398+
expect(response.statusCode).to.equal(504);
399399
});
400400
});
401401

test/unit/handlers.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,21 @@ describe('default proxy error handler', function() {
101101
errorMessage = undefined;
102102
});
103103

104-
it('should set the http status code to: 500', function() {
105-
proxyError(mockError, mockReq, mockRes, proxyOptions);
106-
expect(httpErrorCode).to.equal(500);
104+
var codes = [
105+
['HPE_INVALID_FOO', 502],
106+
['HPE_INVALID_BAR', 502],
107+
['ECONNREFUSED', 504],
108+
['ENOTFOUND', 504],
109+
['ECONNREFUSED', 504],
110+
['any', 500],
111+
];
112+
codes.forEach(function(item) {
113+
var msg = item[0];
114+
var code = item[1];
115+
it('should set the http status code for ' + msg + ' to: ' + code, function() {
116+
proxyError({ code: msg }, mockReq, mockRes, proxyOptions);
117+
expect(httpErrorCode).to.equal(code);
118+
});
107119
});
108120

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

0 commit comments

Comments
 (0)