node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Specific error for timeouts

Open pablote opened this issue 6 years ago • 2 comments

I'm currently proxying requests like this:

this._proxy.web(req, res, { target: target.host, proxyTimeout }, err => {
				logger.error("Error proxying %s -> { Host: %s, URL: %s }: ", req.originalUrl, target.host, target.requestURL);
				logger.error(err);
				res.status(500).send();
			});

If the upstream server takes too long, the callback is run with the following error:

2019-03-28T19:49:22.165Z - error:  message=socket hang up, stack=Error: socket hang up
    at createHangUpError (_http_client.js:331:15)
    at Socket.socketCloseListener (_http_client.js:363:23)
    at emitOne (events.js:121:20)
    at Socket.emit (events.js:211:7)
    at TCP._handle.close [as _onclose] (net.js:557:12)
, code=ECONNRESET

Would it make sense to get a timeout specific error? so I can, if I chose to do so, return a specific timeout error like 504?

pablote avatar Mar 28 '19 19:03 pablote

Guys, any progress with this issue?

Doc999tor avatar Jan 01 '20 14:01 Doc999tor

@pablote and @Doc999tor I have a fix for this in #1650 however it may not be merged or may take some time to get merged based on the activity in this repo. I found a temporary solution which works for me: instead of setting the proxyTimeout option you can set the timeout manually yourself by adding a proxyReq handler:

proxy.on('proxyReq', (proxyReq) => {
  proxyReq.setTimeout(1000, () => {
    const timeoutError = new Error('The proxy request timed out');
    timeoutError.code = 'ETIMEDOUT';
    proxyReq.destroy(timeoutError);
  });
});

rowanmanning avatar Oct 07 '23 12:10 rowanmanning