request: Can't catch error using callback and on-error handler
Hello, I can’t handle error in the following code:
this.request.get({
url: url,
headers: this.headers
}, (err, res, body) => {
if (err) return callback(err);
callback(err, res, body);
}).on('error', err => {
console.log(err.stack);
callback(err);
});
I got error output:
Error: read ECONNRESET
at exports._errnoException (util.js:856:11)
at TCP.onread (net.js:550:26)
After this my process terminates.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 9
- Comments: 33 (2 by maintainers)
Just wondering, why is this issue closed? I have encountered this issue as well, recently.
I’m having this same issue. I would expect that, if a callback is provided, request would handle any connection related exceptions. Instead I’m getting “Unhandled ‘error’ event / ECONNREFUSED”. I’m writing an application that needs to be robust with respect to handling failed connections. Is this a bug, or am I doing it wrong?
If this request fails due to connection refused, the exception is not caught, and my app hard stops on an unhandled exception. We never get to the callback.
Running node v4.4.2 and request v2.7.0
We have same issue with just the same code:
When this code is executed in environments with bad connections it may cause unhadeledException.
This exception It not catched by catch() or .on(‘error’), or callback. The only way to handle such error is using process.on(‘uncacughtException’) which is obviously not good practice.
I think issue is somewhere deeper in node.http module in some async function, because the only way to handle it is process.on(‘uncacughtException’) But I don’t have enough knowledge to investigate this. The most hard part in investigation is in nature of the issue: it’s very hard to simulate such circumstances to reproduce it.
Thank you and sorry for bad english
Edit: @xl-yang As it turns out, the only thing seems to have changed after playing around with node versions is that the errors are now being caught by
process.on('uncaughtException', ...)(this wasn’t catching the errors earlier). I’d left this handler in by accident (with just a debugger statement inside) and it was silently swallowing theECONNRESETerrors until eventually it slowed to a halt and I noticed. So basically: nothing has changed. (Though at least I can catch the errors now and fail somewhat gracefully)Original comment: I’m testing a big list of proxies returned by the npm
proxy-listmodule (see my comment above). It could be that there were a few weird servers causing the problem and that they’ve changed their behaviour or are no longer returned in the lists, but I did try to narrow down on any particular server that was causing the problem but the error just seemed to occur out of nowhere. I really have no idea at this stage! I’ll let you know if I get any more clues.I’m also having this problem. I get this message at
events.jsline number 163:Exact same uncatchable error as @JustPauL is getting, and I’m also testing a large list of proxies. @josser is right that it’s hard to reproduce this. It occurs very rarely and very randomly and it’s hard to get any clues at all even if you “pause on uncaught errors” in the inspector. Here’s the code (in the most minimal form I could make it) that reproduces this problem (download the zip to get the
getNextProxy.jsfile):uncatchable-error.zip
If you run the above script for about 10 minutes (start it with
node --inspect-brk .), this eventually happens:It it runs through the full proxy list without the error you’ll have to run it again. I’m not sure what’s causing it, or how to debug it.
@simov, do you have time to take a quick look at this? Should I post a new issue or can this be re-opened?
Edit 1: I can’t even catch this error with
process.on('uncaughtException', ...)orprocess.on('unhandledRejection', ...). I’ve tested this with node v7.1.0 and v8.1.3 (latest). Continuing investigation and will make further edits if I make any progress.Edit 2: #1946 seems to be closely related to this issue. I’ve tried adding
.end()to the request so that the request doesn’t run viaprocess.nextTick, but this had no effect. Also tried adding a separateon('error', ...)handler, and also wrapped the whole lot in a try/catch. None of that seems to work - still get the same error with the same unhelpful call stack.Edit 3: May also be related to node/3595 and node/14102 and nodejs/help/705.
Edit 4: I have tried adding
as suggested here and here. And have also tried
if(response) response.on('error', error => reject(error));, but the error remains uncaught and the script crashes.Same problem here 😦
I’m using request to test a large set of proxies and sometimes I also get this error:
It happens when I can’t connect to proxy and it returns 403 status.