When a socket hang up is thrown, one of two things happens:
When you're a customer,
When you send a request to a distant server as a client and don't get a response in a timely manner. This error is caused by the end of your socket. You should catch this error and decide what to do with it, such as retrying the request or queueing it for later.
If you're a server or proxy,
When you, as a server, possibly a proxy server, get a request from a client and begin acting on it (or relaying the request to the upstream server), the client decides to cancel/abort the request before you have finished preparing the response.
When a customer cancels a request, this stack trace depicts what happened.
Trace: { [Error: socket hang up] code: 'ECONNRESET' }
at ClientRequest.proxyError (your_server_code_error_handler.js:137:15)
at ClientRequest.emit (events.js:117:20)
at Socket.socketCloseListener (http.js:1526:9)
at Socket.emit (events.js:95:17)
at TCP.close (net.js:465:12)
Line http.js:1526:9points to the same socketCloseListener mentioned by @Blender, particularly:
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.emit('error', createHangUpError());
...
function createHangUpError() {
var error = new Error('socket hang up');
error.code = 'ECONNRESET';
return error;
}
If the client is a browser user, this is a common scenario. When a request for a resource/page takes a long time to load, visitors simply refresh the page. As a result of this action, the previous request is cancelled, resulting in this error on your server.
Because this issue is the result of a client's request, they should not expect to receive an error notice. As a result, there's no reason to consider this error to be important. Simply disregard it. This is bolstered by the fact that on such an error, the res socket that your client was listening to is destroyed, despite the fact that it is still editable.
console.log(res.socket.destroyed); //true
So, no point to send anything, except explicitly closing the response object:
res.end();
However, if you are a proxy server that has already transmitted the request to the upstream, you should abort your internal request to the upstream, signalling that you are uninterested in the response, which will alert the upstream server to maybe halt an expensive activity.
To know more about Node JS, It's recommended to join Node JS Online Course today.