http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.92k stars 1.97k forks source link

Specific error for timeouts #1331

Open pablote opened 5 years ago

pablote commented 5 years ago

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?

Doc999tor commented 4 years ago

Guys, any progress with this issue?

rowanmanning commented 1 year ago

@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);
  });
});