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?

4

1 回答 1

0

摘自:https ://stackoverflow.com/a/4328705/4478897

注意:这篇文章有点旧

下一个例子是,http.createClient但我认为它可能是一样的

不幸的是,目前还没有办法直接捕获这些异常,因为所有的事情都是在后台异步发生的。

您所能做的就是自己捕获 uncaughtException:

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

也许这对你有帮助!

更多:https ://stackoverflow.com/a/19793797/4478897

更新:

您是否尝试更改log.error()console.error()???

于 2015-07-24T13:35:48.650 回答