javascript 如何处理 ECONNRESET、对等方重置连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:20:32  来源:igfitidea点击:

How to handle ECONNRESET, Connection reset by peer

javascriptnode.js

提问by Swizec Teller

A lot of my node.js processes are crashing with the ECONNRESET error. Here's the output I can see:

我的很多 node.js 进程都因 ECONNRESET 错误而崩溃。这是我可以看到的输出:

node.js:50
    throw e;
    ^
Error: ECONNRESET, Connection reset by peer
    at Client._readImpl (net:320:14)
    at IOWatcher.callback (net:470:24)
    at node.js:607:9

Does anyone know how to handle this? It's not a very useful stack trace so I have no idea where it's happening. Should I just wrap any and all access to a remote source via http with a try/catch block? Or is there a better way?

有谁知道如何处理这个问题?这不是一个非常有用的堆栈跟踪,所以我不知道它发生在哪里。我应该使用 try/catch 块包装通过 http 对远程源的任何和所有访问吗?或者,还有更好的方法?

In general I don't care if this does happen or if some task does not get completed because of this. What I do care about is that the process should just shrug it off and work on the next task.

一般来说,我不在乎这是否会发生,或者某些任务是否因此而没有完成。我真正关心的是,这个过程应该只是耸耸肩,继续下一个任务。

回答by kanaka

You need to attach to the error Event for your socket. If you don't, then the default action is to throw an exception when an error occurs.

您需要附加到套接字的错误事件。如果不这样做,则默认操作是在发生错误时抛出异常。

socket.on('error', function (exc) {
    sys.log("ignoring exception: " + exc);
});