Skip to content

Commit c5f9627

Browse files
committed
http: don't error after http client response
1 parent c1f0cbe commit c5f9627

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

doc/api/errors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,11 @@ instance.setEncoding('utf8');
17171717
An attempt was made to call [`stream.write()`][] after `stream.end()` has been
17181718
called.
17191719

1720+
<a id="ERR_REQUEST_WRITE_AFTER_RESPONSE"></a>
1721+
### ERR_REQUEST_WRITE_AFTER_RESPONSE
1722+
1723+
An attempt was made to call [`req.write()`][] after `'response'` has been emitted.
1724+
17201725
<a id="ERR_STRING_TOO_LONG"></a>
17211726
### ERR_STRING_TOO_LONG
17221727

lib/_http_client.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const {
4646
ERR_INVALID_ARG_TYPE,
4747
ERR_INVALID_HTTP_TOKEN,
4848
ERR_INVALID_PROTOCOL,
49-
ERR_UNESCAPED_CHARACTERS
49+
ERR_UNESCAPED_CHARACTERS,
50+
ERR_REQUEST_WRITE_AFTER_RESPONSE
5051
} = codes;
5152
const { getTimerDuration } = require('internal/timers');
5253
const {
@@ -302,6 +303,22 @@ ClientRequest.prototype._finish = function _finish() {
302303
OutgoingMessage.prototype._finish.call(this);
303304
};
304305

306+
ClientRequest.prototype.write = function write(chunk, encoding, callback) {
307+
if (this.res) {
308+
this.emit('error', new ERR_REQUEST_WRITE_AFTER_RESPONSE());
309+
return true;
310+
}
311+
OutgoingMessage.prototype.write.call(this, chunk, encoding, callback);
312+
};
313+
314+
ClientRequest.prototype.end = function end(chunk, encoding, callback) {
315+
if (this.res) {
316+
this.emit('error', new ERR_REQUEST_WRITE_AFTER_RESPONSE());
317+
return this;
318+
}
319+
OutgoingMessage.prototype.end.call(this, chunk, encoding, callback);
320+
};
321+
305322
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
306323
if (this._header) {
307324
throw new ERR_HTTP_HEADERS_SENT('render');

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ E('ERR_OUT_OF_RANGE',
10521052
msg += ` It must be ${range}. Received ${received}`;
10531053
return msg;
10541054
}, RangeError);
1055+
E('ERR_REQUEST_WRITE_AFTER_RESPONSE', 'write after response', Error);
10551056
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
10561057
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
10571058
'Script execution was interrupted by `SIGINT`', Error);

0 commit comments

Comments
 (0)