Skip to content

Commit

Permalink
stream: migrate _stream_readable use error codes
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
benhalverson authored and mcollina committed Oct 29, 2017
1 parent de61f97 commit 88fb359
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
21 changes: 21 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
}));
12 changes: 10 additions & 2 deletions test/parallel/test-stream-unshift-read-race.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}));
}


Expand All @@ -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();
}));

Expand Down

0 comments on commit 88fb359

Please sign in to comment.