0

I am using the library request to post data but I constantly get the error ECONNRESET even though I have an error event handler for it. Sometimes my code does indeed handle the error, but not everytime.

I have this in a loop:

request.post(url, {form:{data}}).on('error',
  function(err){ 
    if(err === 'ECONNRESET'){ 
       console.log('ECONNRESET') 
    }
})

And, as described, I still get this error:

        throw er; // Unhandled 'error' event
        ^ Error: read ECONNRESET
gaaaaaa
  • 336
  • 1
  • 14
  • Try logging `err.message` and `err.stack` maybe that gives you more information about the nature of the error. Also [search the web, e.g.](https://www.quora.com/What-does-%E2%80%9CError-read-ECONNRESET%E2%80%9D-mean) – KooiInc May 15 '20 at 16:34
  • @KooiInc Well, I don't think I can use err.message because the error itself isn't caught (?). But when my code catches it, I get: "tunneling socket could not be established, cause=read ECONNRESET" Thanks. – gaaaaaa May 15 '20 at 16:49
  • I would be surprised if the `err` from the `on("error" , ...)` handler wouldn't be some kind of `Error` object. – KooiInc May 15 '20 at 17:00
  • @KooiInc Well, I mean, what else could be ? I have an ```on("error", ...)``` handler but I still get ```Unhandled 'error' event```. I have tried removing this part of the code (which is the main part of it) to see if I would still get this error, and I didn't get the error. Thank you for answering :) – gaaaaaa May 15 '20 at 17:06

2 Answers2

3

Seems like .on('error', ...); can't catch ECONNRESET after N times so the only way to solve if (afaik now) is using process.on('uncaughtException', function(error) {})

I hope this help other people :)

gaaaaaa
  • 336
  • 1
  • 14
1

Wrap all the code you have using request in a try {} clause, followed by

catch (error) {
  console.error (error)
}

From something you wrote in a comment, it seems possible something is wrong with your proxy server. Can you make the request without it? If so, try that.

You also might try using the command line program called curl to make a similar request. curl --verbose will chatter about what it does, letting you see some details about what is failing. You didn't give us enough information to allow a detailed suggestion about how to use curl. This may help with the proxy settings. performing HTTP requests with cURL (using PROXY)

O. Jones
  • 103,626
  • 17
  • 118
  • 172