From c0465520a53c51d49308c2cefccc5d63ff07c92e Mon Sep 17 00:00:00 2001 From: Ben Halverson Date: Sat, 26 Aug 2017 00:53:27 -0700 Subject: [PATCH] stream: migrate _stream_readable use error codes PR-URL: https://github.com/nodejs/node/pull/15042 Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson Reviewed-By: Refael Ackermann Reviewed-By: Gireesh Punathil Reviewed-By: Joyee Cheung --- doc/api/errors.md | 21 +++++++++++++++++++ lib/_stream_readable.js | 8 ++++--- lib/internal/errors.js | 3 +++ ...tream-readable-with-unimplemented-_read.js | 8 +++++-- .../parallel/test-stream-unshift-read-race.js | 12 +++++++++-- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 498fc856ad..77da558371 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1317,6 +1317,24 @@ Node.js does not allow `stdout` or `stderr` Streams to be closed by user code. Used when an attempt is made to close the `process.stdout` stream. By design, Node.js does not allow `stdout` or `stderr` Streams to be closed by user code. + +### ERR_STREAM_PUSH_AFTER_EOF + +Used when an attempt is made to call [`stream.push()`][] after a `null`(EOF) +has been pushed to the stream. + + +### ERR_STREAM_READ_NOT_IMPLEMENTED + +Used when an attempt is made to use a readable stream that has not implemented +[`readable._read()`][]. + + +### ERR_STREAM_UNSHIFT_AFTER_END_EVENT + +Used when an attempt is made to call [`stream.unshift()`][] after the +`end` event has been emitted. + ### ERR_STREAM_WRAP @@ -1462,7 +1480,10 @@ closed. [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE [`hash.digest()`]: crypto.html#crypto_hash_digest_encoding [`hash.update()`]: crypto.html#crypto_hash_update_data_inputencoding +[`readable._read()`]: stream.html#stream_readable_read_size_1 [`sign.sign()`]: crypto.html#crypto_sign_sign_privatekey_outputformat +[`stream.push()`]: stream.html#stream_readable_push_chunk_encoding +[`stream.unshift()`]: stream.html#stream_readable_unshift_chunk [`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal [`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback [`fs.readFileSync`]: fs.html#fs_fs_readfilesync_path_options diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 78732ed185..deea864647 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -31,6 +31,7 @@ const util = require('util'); const debug = util.debuglog('stream'); const BufferList = require('internal/streams/BufferList'); const destroyImpl = require('internal/streams/destroy'); +const errors = require('internal/errors'); var StringDecoder; util.inherits(Readable, Stream); @@ -233,11 +234,12 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { if (addToFront) { if (state.endEmitted) - stream.emit('error', new Error('stream.unshift() after end event')); + stream.emit('error', + new errors.Error('ERR_STREAM_UNSHIFT_AFTER_END_EVENT')); else addChunk(stream, state, chunk, true); } else if (state.ended) { - stream.emit('error', new Error('stream.push() after EOF')); + stream.emit('error', new errors.Error('ERR_STREAM_PUSH_AFTER_EOF')); } else { state.reading = false; if (state.decoder && !encoding) { @@ -548,7 +550,7 @@ function maybeReadMore_(stream, state) { // for virtual (non-string, non-buffer) streams, "length" is somewhat // arbitrary, and perhaps not very meaningful. Readable.prototype._read = function(n) { - this.emit('error', new Error('_read() is not implemented')); + this.emit('error', new errors.Error('ERR_STREAM_READ_NOT_IMPLEMENTED')); }; Readable.prototype.pipe = function(dest, pipeOpts) { diff --git a/lib/internal/errors.js b/lib/internal/errors.js index faeb76e9ba..be3d09bb5e 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -325,6 +325,9 @@ E('ERR_SOCKET_CLOSED', 'Socket is closed'); E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running'); E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed'); E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed'); +E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF'); +E('ERR_STREAM_READ_NOT_IMPLEMENTED', '_read() is not implemented'); +E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event'); E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode'); E('ERR_TLS_CERT_ALTNAME_INVALID', 'Hostname/IP does not match certificate\'s altnames: %s'); diff --git a/test/parallel/test-stream-readable-with-unimplemented-_read.js b/test/parallel/test-stream-readable-with-unimplemented-_read.js index ce325c0823..b644bd4067 100644 --- a/test/parallel/test-stream-readable-with-unimplemented-_read.js +++ b/test/parallel/test-stream-readable-with-unimplemented-_read.js @@ -1,8 +1,12 @@ 'use strict'; -require('../common'); +const common = require('../common'); const stream = require('stream'); const assert = require('assert'); const readable = new stream.Readable(); -assert.throws(() => readable.read(), /not implemented/); +assert.throws(() => readable.read(), common.expectsError({ + code: 'ERR_STREAM_READ_NOT_IMPLEMENTED', + type: Error, + message: '_read() is not implemented' +})); diff --git a/test/parallel/test-stream-unshift-read-race.js b/test/parallel/test-stream-unshift-read-race.js index e04de5392a..a67147506e 100644 --- a/test/parallel/test-stream-unshift-read-race.js +++ b/test/parallel/test-stream-unshift-read-race.js @@ -70,7 +70,11 @@ r._read = function(n) { function pushError() { assert.throws(function() { r.push(Buffer.allocUnsafe(1)); - }, /^Error: stream\.push\(\) after EOF$/); + }, common.expectsError({ + code: 'ERR_STREAM_PUSH_AFTER_EOF', + type: Error, + message: 'stream.push() after EOF' + })); } @@ -84,7 +88,11 @@ w._write = function(chunk, encoding, cb) { r.on('end', common.mustCall(function() { assert.throws(function() { r.unshift(Buffer.allocUnsafe(1)); - }, /^Error: stream\.unshift\(\) after end event$/); + }, common.expectsError({ + code: 'ERR_STREAM_UNSHIFT_AFTER_END_EVENT', + type: Error, + message: 'stream.unshift() after end event' + })); w.end(); }));