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

Do Not Proxy Request When Custom Router Throws Error #411

Closed
bforbis opened this issue Feb 28, 2020 · 5 comments · Fixed by #413
Closed

Do Not Proxy Request When Custom Router Throws Error #411

bforbis opened this issue Feb 28, 2020 · 5 comments · Fixed by #413

Comments

@bforbis
Copy link
Contributor

bforbis commented Feb 28, 2020

I am trying to take complete control of the target routing. I would like the proxied request to be aborted before the client socket is opened if my router was unable to determine a target host. Is this possible to do?

createProxyMiddleware({
  // target is a required parameter
  target: 'do-not-use', 
  router: (req) => {
    try {
      ...
      return target;
    }
    catch (err) {
      req.res.sendStatus(500);
    }
  },
});

The current http-proxy code will still use the original target "do-not-use". I would rather the proxy just abort the request entirely if the router function does not return a target.

@chimurai
Copy link
Owner

chimurai commented Feb 29, 2020

Currently it is not possible.

How do you use the http-proxy-middleware?

Via webpack-dev-server (create-react-app, angular-cli, vue-cli...)? Or do you mount it in your own express or express-like server?

@bforbis
Copy link
Contributor Author

bforbis commented Feb 29, 2020

This is in an express server. Looking through the code in prepareProxyRequest, it seems like there should be some error handling there to catch errors.

The two error return types I can think of for a custom router causes two separate issues.

  1. Return empty string from router: This causes the proxy to use the default target "do-not-use", which will throw a socket connection error
  2. Throw an error in the router: This causes an uncaught promise rejection error at the NodeJS level, since there is no error handling in the proxy-middleware code.

@chimurai
Copy link
Owner

chimurai commented Mar 1, 2020

You could use express's error handling: https://expressjs.com/en/guide/error-handling.html

Basically throw an error in the router and use a default middleware error handler to handle all sorts of errors from middlewares.

import * as express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';

const app = express();

const proxy = createProxyMiddleware({
  // target is a required parameter
  target: 'do-not-use', 
  router: (req) => {
    try {
      ...
      return target;
    }
    catch (err) {
      throw err;
    }
  },
});

app.use(proxy);
app.use((err, req, res, next) => {
  res.send(500, 'oops some routing went wrong')
});

@bforbis
Copy link
Contributor Author

bforbis commented Mar 12, 2020

I tried this out, but I am still getting UnhandledPromiseRejection errors logged to the console. Should the code that calls prepareProxyRequest catch the error and then pass it to the next() function?

try {
  const activeProxyOptions = await this.prepareProxyRequest(req);
  this.proxy.web(req, res, activeProxyOptions);
}
catch(err) {
  next(err);
}

@chimurai
Copy link
Owner

Was hoping that UnhandledPromiseRejection wouldn't happen.

Your suggestion to solve it makes sense than.

Do you mind creating PR with your suggestion to fix this issue?

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.

2 participants