Skip to content

Commit d3ac18a

Browse files
b0yfriendjoyeecheung
authored andcommitted
lib: migrate _http_outgoing.js's remaining errors
A couple of lib/_http_outgoing.js's errors were still in the "old style": `throw new Error(<some message here>)`. This commit migrates those 2 old style errors to the "new style": internal/errors.js's error-system. In the future, changes to these errors' messages won't break semver-major status. With the old style, changes to these errors' messages broke semver-major status. It was inconvenient. Refs: #17709 PR-URL: #17837 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9f122e3 commit d3ac18a

5 files changed

+18
-16
lines changed

lib/_http_outgoing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
629629

630630
function write_(msg, chunk, encoding, callback, fromEnd) {
631631
if (msg.finished) {
632-
var err = new Error('write after end');
632+
const err = new errors.Error('ERR_STREAM_WRITE_AFTER_END');
633633
nextTick(msg.socket && msg.socket[async_id_symbol],
634634
writeAfterEndNT.bind(msg),
635635
err,
@@ -880,7 +880,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
880880

881881
OutgoingMessage.prototype.pipe = function pipe() {
882882
// OutgoingMessage should be write-only. Piping from it is disabled.
883-
this.emit('error', new Error('Cannot pipe, not readable'));
883+
this.emit('error', new errors.Error('ERR_STREAM_CANNOT_PIPE'));
884884
};
885885

886886
module.exports = {

test/parallel/test-http-res-write-after-end.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ const assert = require('assert');
2525
const http = require('http');
2626

2727
const server = http.Server(common.mustCall(function(req, res) {
28-
res.on('error', common.mustCall(function onResError(err) {
29-
assert.strictEqual(err.message, 'write after end');
28+
res.on('error', common.expectsError({
29+
code: 'ERR_STREAM_WRITE_AFTER_END',
30+
type: Error
3031
}));
3132

3233
res.write('This should write.');

test/parallel/test-http-server-write-after-end.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
const common = require('../common');
44
const http = require('http');
5-
const assert = require('assert');
65

76
// Fix for https://github.com/nodejs/node/issues/14368
87

98
const server = http.createServer(handle);
109

1110
function handle(req, res) {
1211
res.on('error', common.mustCall((err) => {
13-
assert.strictEqual(err.message, 'write after end');
12+
common.expectsError({
13+
code: 'ERR_STREAM_WRITE_AFTER_END',
14+
type: Error
15+
})(err);
1416
server.close();
1517
}));
1618

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
2-
const assert = require('assert');
32
const common = require('../common');
43
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
54

65
// Verify that an error is thrown upon a call to `OutgoingMessage.pipe`.
76

87
const outgoingMessage = new OutgoingMessage();
9-
assert.throws(
10-
common.mustCall(() => { outgoingMessage.pipe(outgoingMessage); }),
11-
(err) => {
12-
return ((err instanceof Error) && /Cannot pipe, not readable/.test(err));
13-
},
14-
'OutgoingMessage.pipe should throw an error'
8+
common.expectsError(
9+
() => { outgoingMessage.pipe(outgoingMessage); },
10+
{
11+
code: 'ERR_STREAM_CANNOT_PIPE',
12+
type: Error
13+
}
1514
);

test/parallel/test-pipe-outgoing-message-data-emitted-after-ended.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const common = require('../common');
33
const http = require('http');
4-
const assert = require('assert');
54
const stream = require('stream');
65

76
// Verify that when piping a stream to an `OutgoingMessage` (or a type that
@@ -17,8 +16,9 @@ const server = http.createServer(common.mustCall(function(req, res) {
1716
process.nextTick(common.mustCall(() => {
1817
res.end();
1918
myStream.emit('data', 'some data');
20-
res.on('error', common.mustCall(function(err) {
21-
assert.strictEqual(err.message, 'write after end');
19+
res.on('error', common.expectsError({
20+
code: 'ERR_STREAM_WRITE_AFTER_END',
21+
type: Error
2222
}));
2323

2424
process.nextTick(common.mustCall(() => server.close()));

0 commit comments

Comments
 (0)