Skip to content

Commit 5bb4d01

Browse files
ShogunPandamcollina
authored andcommitted
doc: add note about clientError writable handling
PR-URL: #33308 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 0ddae48 commit 5bb4d01

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

doc/api/http.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,21 @@ ensure the response is a properly formatted HTTP response message.
10451045
correctly;
10461046
* `rawPacket`: the raw packet of current request.
10471047

1048+
In some cases, the client has already received the response and/or the socket
1049+
has already been destroyed, like in case of `ECONNRESET` errors. Before
1050+
trying to send data to the socket, it is better to check that it is still
1051+
writable.
1052+
1053+
```js
1054+
server.on('clientError', (err, socket) => {
1055+
if (err.code === 'ECONNRESET' || !socket.writable) {
1056+
return;
1057+
}
1058+
1059+
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
1060+
});
1061+
```
1062+
10481063
### Event: `'close'`
10491064
<!-- YAML
10501065
added: v0.1.4

0 commit comments

Comments
 (0)