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

feat: allow sending a custom proxy timeout error #1650

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Oct 7, 2023

  1. feat: allow sending a custom proxy timeout error

    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 committed Oct 7, 2023
    Configuration menu
    Copy the full SHA
    23aeb28 View commit details
    Browse the repository at this point in the history