[Node.js] How to handle ECONNRESET, Connection reset by peer and automatic crashing.

Error Message
    throw e;
Error: ECONNRESET, Connection reset by peer
    at Stream._readImpl (net:304:14)
    at IOWatcher.callback (net:454:24)
    at node.js:768:9

The error 'ECONNRESET, Connection reset by peer' like the above is thrown when a connection was forcibly closed by peer. For example, a sudden lost of connection due to unstable network environment or unexpected crash of a browser, etc. 
If such exception bubbles all the way back to event loop, the node process print a stack trace and exit as the default action. Try/catch blocks can't catch the excption which occurs in event loop.
To prevent these errors from crashing down your node server, please use

process.on('uncaughtException', function (err) {
  // handle the error

[nodejs.org] Event: 'uncautException'
[nodejs@googlegroups.com] How to prevent node from dying on error?
[technet.microsoft.com] Connection reset by peer

0 件のコメント: