1

When a remote site is off-line I am getting this error in my consuming client (Node.js v0.12.0 with the http module):

 Uncaught exception: connect ECONNREFUSED
 Error: connect ECONNREFUSED
     at exports._errnoException (util.js:746:11)
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:983:19)

The code I'm currently using looks like this:

var req = http.request(options, function (res) {
    res.on('socket', function (socket) {
        socket.setKeepAlive(true, 0);
        socket.setNoDelay(true);
    });

    res.on('end', function () {
        log.debug('Success');
    }).on('error', function () {
        log.error('Response parsing failed');
    });
}).on('error', function () {
    log.error('HTTP request failed');
});

req.write(packet);
req.end();

The "error" event is never fired when the ECONNREFUSED occurs, I've tried using the "clientError" event but this is not fired either.

How can I capture this error?

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • Are you using io.js? – thefourtheye Jul 16 '15 at 12:47
  • No I'm not currently – seanhodges Jul 16 '15 at 12:50
  • have you tried to call req.end(); at the end of your code? The documentation stated hier: https://nodejs.org/api/http.html#http_http_request_options_callback: "Note that in the example req.end() was called. With http.request() one must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body." – solick Jul 16 '15 at 14:34
  • @solick great question. I do actually have `req.end()` by I cut it out when condensing my example code for this question. I'll put it back. – seanhodges Jul 16 '15 at 15:26

1 Answers1

0

Extracted from: https://stackoverflow.com/a/4328705/4478897

NOTE: This post is a bit old

The next example is with the http.createClient but i think it could be the same

Unfortunately, at the moment there's no way to catch these exceptions directly, since all the stuff happens asynchronously in the background.

All you can do is to catch the uncaughtException's on your own:

process.on('uncaughtException', function (err) {
    console.log(err);
}); 

Maybe that helps you!

More this: https://stackoverflow.com/a/19793797/4478897

UPDATE:

did you tried to change log.error() to console.error() ???

Community
  • 1
  • 1
nada
  • 972
  • 5
  • 22