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

Catch proxy error? #264

Closed
alanhoff opened this issue Jun 16, 2012 · 9 comments
Closed

Catch proxy error? #264

alanhoff opened this issue Jun 16, 2012 · 9 comments

Comments

@alanhoff
Copy link

Is there a way to catch proxy error? I mean, if the target didn't respond or isn't availabe..
I was thinking about something like this:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {

  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });

  proxy.on('error', function(err, req, res){
      if(err) console.log(err);
      res.writeHead(500);
      res.end('Ooops, something went very wrong...');
  });

}).listen(80);
@cronopio
Copy link
Contributor

In this example you will see the use of proxyError event.

@alanhoff
Copy link
Author

Worked like a charm, thx.

@alum
Copy link

alum commented Jul 15, 2013

This does not work well for the RoutingProxy.

If I get ECONNRESET from the backend server, doing res.writeHead(500) in the proxyError handler will give me the "Can't render headers after they are sent to the client." error.

Any ideas?

EDIT: Actually, the proxy already resets the connection for me, but does not give me the alternative to call res.send(500)

@mmalecki
Copy link
Contributor

@alum this is expected. When you start proxying and the backend server sends the headers and then breaks the connections, the headers were already proxied back to the client. You have to catch the "Can't render headers after they are sent to the client." error.

@revskill10
Copy link

Hi, i've met this error in my proxy, i follow your guide to catch the error like this:

var proxy = new httpProxy.RoutingProxy();
proxy.on("Can't render headers after they are sent to the client.", function(err, req, res){
if(err) console.log(err);
res.writeHead(500);
res.end('Ooops, something went very wrong....');
});

But the proxy still throws the error, and i can't caught it.
Do you have any advice ?
THank you very much.

@alum
Copy link

alum commented Dec 18, 2013

So it's actually not the proxy that's throwing that error, but rather the response object. Also, there is not event called "Can't render headers after they are sent to the client." Most events are called things like "error" or "finish".
Basically I think you need to wrap res.writeHead() in a try-catch and handle the error that way.

@ghost
Copy link

ghost commented Feb 18, 2014

Hi all, is there any way to identify which backend server failed to serve the request, so that we can mark that server as down.

@alum
Copy link

alum commented Feb 19, 2014

@arjunrp
You probably want to set up one proxy per backend server. That way you will always know which server failed and you will have a nice separation between proxies.

@ghost
Copy link

ghost commented Feb 20, 2014

@alum thanks for the reply, i have got another solution
proxy.web(req, res, target, function (e) {
// check the error object and mark the target as down
});
is this the correct way to do this?

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

No branches or pull requests

5 participants