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

Specific error for timeouts #1331

Open
pablote opened this issue Mar 28, 2019 · 2 comments · May be fixed by #1650
Open

Specific error for timeouts #1331

pablote opened this issue Mar 28, 2019 · 2 comments · May be fixed by #1650

Comments

@pablote
Copy link

pablote commented Mar 28, 2019

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
Copy link

Guys, any progress with this issue?

rowanmanning added a commit to rowanmanning/node-http-proxy that referenced this issue Oct 7, 2023
When using `proxyTimeout` it's very difficult to tell the difference
between a regular socket hangup and a timeout because, in both cases, an
`ECONNRESET` error is thrown. Ideally we should be able to identify when
a proxy request has failed because it took too long, this would allow us
to do things like send appropriate `504` status code.

Suddenly throwing a different error would probably be considered a
breaking change because it's possible that users of http-proxy are
relying on the `ECONNRESET` error. I decided to add the custom timeout
error behind a new option for now so that people can opt into using it.

If you set this option:

```js
var proxy = httpProxy.createProxyServer({
  target: 'http://example.com',
  proxyTimeout: 100,
  proxyTimeoutCustomError: true
});
```

Then the error that gets thrown will have a message of `"The proxy
request timed out"` and a code of `ETIMEDOUT` to match Node.js:
https://nodejs.org/api/errors.html#common-system-errors

This allows for custom error handling code like this:

```js
proxy.on('error', function(err, req, res) {
  if (err.code === 'ETIMEDOUT') {
    res.writeHead(504);
  } else {
    res.writeHead(503);
  }
  // ...
});
```

Resolves http-party#1331.
@rowanmanning rowanmanning linked a pull request Oct 7, 2023 that will close this issue
@rowanmanning
Copy link

rowanmanning commented Oct 7, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants