Skip to content

Commit

Permalink
lib: migrate _http_outgoing.js's remaining errors
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
b0yfriend authored and joyeecheung committed Dec 27, 2017
1 parent 9f122e3 commit d3ac18a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {

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

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

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-http-res-write-after-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const assert = require('assert');
const http = require('http');

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

res.write('This should write.');
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-http-server-write-after-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

const common = require('../common');
const http = require('http');
const assert = require('assert');

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

const server = http.createServer(handle);

function handle(req, res) {
res.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'write after end');
common.expectsError({
code: 'ERR_STREAM_WRITE_AFTER_END',
type: Error
})(err);
server.close();
}));

Expand Down
13 changes: 6 additions & 7 deletions test/parallel/test-outgoing-message-pipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';
const assert = require('assert');
const common = require('../common');
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;

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

const outgoingMessage = new OutgoingMessage();
assert.throws(
common.mustCall(() => { outgoingMessage.pipe(outgoingMessage); }),
(err) => {
return ((err instanceof Error) && /Cannot pipe, not readable/.test(err));
},
'OutgoingMessage.pipe should throw an error'
common.expectsError(
() => { outgoingMessage.pipe(outgoingMessage); },
{
code: 'ERR_STREAM_CANNOT_PIPE',
type: Error
}
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const stream = require('stream');

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

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

0 comments on commit d3ac18a

Please sign in to comment.