diff options
author | Ben Halverson <benhalverson33@gmail.com> | 2017-08-26 00:53:27 -0700 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2017-10-29 12:29:29 +0100 |
commit | 88fb359c578b5d1a58885f3afcf6e542a9f07843 (patch) | |
tree | 0e4ec2e808dba1aae0320013350d663f01d6ff91 | |
parent | de61f97c3dbc1a07911349f8360f94be127ca28a (diff) | |
download | node-new-88fb359c578b5d1a58885f3afcf6e542a9f07843.tar.gz |
stream: migrate _stream_readable use error codes
PR-URL: https://github.com/nodejs/node/pull/15042
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
-rw-r--r-- | doc/api/errors.md | 21 | ||||
-rw-r--r-- | lib/_stream_readable.js | 8 | ||||
-rw-r--r-- | lib/internal/errors.js | 3 | ||||
-rw-r--r-- | test/parallel/test-stream-readable-with-unimplemented-_read.js | 8 | ||||
-rw-r--r-- | test/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. +<a id="ERR_STREAM_PUSH_AFTER_EOF"></a> +### 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. + +<a id="ERR_STREAM_READ_NOT_IMPLEMENTED"></a> +### ERR_STREAM_READ_NOT_IMPLEMENTED + +Used when an attempt is made to use a readable stream that has not implemented +[`readable._read()`][]. + +<a id="ERR_STREAM_UNSHIFT_AFTER_END_EVENT"></a> +### ERR_STREAM_UNSHIFT_AFTER_END_EVENT + +Used when an attempt is made to call [`stream.unshift()`][] after the +`end` event has been emitted. + <a id="ERR_STREAM_WRAP"></a> ### 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 fc6b11a40e..a01d9b30cd 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(); })); |