node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

How can I filter socket hang up errors?

Open JSteunou opened this issue 11 years ago • 23 comments

I'm using node-http-proxy for a while now, and I think I'm using it quite right catching errors like this

proxy.on('error', function(err, req, res) {
    // log
    // ...
    // handle
    if (!res.headersSent) {
        res.writeHead(500, { 'content-type': 'application/json' });
    }
    res.end(JSON.stringify({ error: 'proxy_error', reason: err.message }));
});

But I can have two cases that make a "socket hang up" error happen.

  1. Crash behind
  2. Client cancelling the request

Watching at my log I can make no differences between those two cases and it's quite frustrating. Before knowing about request abort (thank #527) I really though I have an error going on.

So, what's the best practice to filter the request.abort case out?

JSteunou avatar Apr 27 '15 15:04 JSteunou

+1

theoephraim avatar Apr 28 '15 15:04 theoephraim

+1

FoghostCn avatar Jul 16 '15 09:07 FoghostCn

You should be able to use the proxy error event or the error callback and check for res.socket.destroyed === true and error.code === ECONNRESET which means the request was canceled

vvo avatar Dec 01 '15 22:12 vvo

Merci @vvo ;) Will try that asap

JSteunou avatar Dec 02 '15 08:12 JSteunou

It works very fine, thank you again @vvo

@jcrugzz should I let this issue open? Maybe http-proxy should filter those error inside the library, or maybe it's better to let it unopinionated...

JSteunou avatar Dec 02 '15 09:12 JSteunou

or maybe it's better to let it unopinionated...

Yes it depends on the use case, sometime you will want the error to pop out if the client canceled the request.

vvo avatar Dec 02 '15 09:12 vvo

What I thought. Closing it then.

JSteunou avatar Dec 02 '15 09:12 JSteunou

Quick question again: I want to use request abort to prevent the underlying server to handle that client cancelled request. But, very surprisingly, req.abort is undefined O_O

Any thoughts?

JSteunou avatar Dec 02 '15 10:12 JSteunou

nope sorry,

vvo avatar Dec 02 '15 10:12 vvo

I think the req I got is the received request, not the request currently sent by http-proxy

JSteunou avatar Dec 02 '15 11:12 JSteunou

Ok for the record, in case anyone was interested at doing the same thing

How to catch request client cancelled and abort ongoing proxied request

proxy.on('proxyReq', function(proxyReq, req) {
    // keep a ref
    req._proxyReq = proxyReq;
});

proxy.on('error', function(err, req, res) {
  if (req.socket.destroyed && err.code === 'ECONNRESET') {
    req._proxyReq.abort();
  }
});

JSteunou avatar Dec 02 '15 11:12 JSteunou

This is a bug and should be fixed within http-proxy itself. Currently we are emitting an error on the client request when we should only be cancelling the proxy request when this happens.

jcrugzz avatar Dec 02 '15 14:12 jcrugzz

Which one? Getting all errors even from client cancelled request or be forced to tricky save proxyReq to be able to abort?

JSteunou avatar Dec 02 '15 15:12 JSteunou

@JSteunou To put it simply, your workaround here should be handled within http-proxy for the req.on('error')

jcrugzz avatar Dec 02 '15 15:12 jcrugzz

Would be nice :)

JSteunou avatar Dec 02 '15 15:12 JSteunou

@JSteunou Pull requests always welcome if you want to take a stab at it! :)

jcrugzz avatar Dec 02 '15 15:12 jcrugzz

also @vvo if you want the error handled, you could handle the error directly on req.on('error'). Im of the opinion that we need to cleanup our proxyReq regardless and if you want it as an error in your code the error can be handled there. Does this seem reasonable?

jcrugzz avatar Dec 02 '15 15:12 jcrugzz

Yep makes sense

vvo avatar Dec 02 '15 15:12 vvo

@jcrugzz I'm afraid I have the necessary solution and motivation but not the time to do it.

JSteunou avatar Dec 02 '15 17:12 JSteunou

@JSteunou it would be a simple change right around here. You may be overestimating the time :). It would be a good opportunity for first contribution

jcrugzz avatar Dec 02 '15 18:12 jcrugzz