Skip to content

Commit 88fb359

Browse files
benhalversonmcollina
authored andcommitted
stream: migrate _stream_readable use error codes
PR-URL: #15042 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent de61f97 commit 88fb359

5 files changed

+45
-7
lines changed

doc/api/errors.md

+21
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,24 @@ Node.js does not allow `stdout` or `stderr` Streams to be closed by user code.
13171317
Used when an attempt is made to close the `process.stdout` stream. By design,
13181318
Node.js does not allow `stdout` or `stderr` Streams to be closed by user code.
13191319

1320+
<a id="ERR_STREAM_PUSH_AFTER_EOF"></a>
1321+
### ERR_STREAM_PUSH_AFTER_EOF
1322+
1323+
Used when an attempt is made to call [`stream.push()`][] after a `null`(EOF)
1324+
has been pushed to the stream.
1325+
1326+
<a id="ERR_STREAM_READ_NOT_IMPLEMENTED"></a>
1327+
### ERR_STREAM_READ_NOT_IMPLEMENTED
1328+
1329+
Used when an attempt is made to use a readable stream that has not implemented
1330+
[`readable._read()`][].
1331+
1332+
<a id="ERR_STREAM_UNSHIFT_AFTER_END_EVENT"></a>
1333+
### ERR_STREAM_UNSHIFT_AFTER_END_EVENT
1334+
1335+
Used when an attempt is made to call [`stream.unshift()`][] after the
1336+
`end` event has been emitted.
1337+
13201338
<a id="ERR_STREAM_WRAP"></a>
13211339
### ERR_STREAM_WRAP
13221340

@@ -1462,7 +1480,10 @@ closed.
14621480
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE
14631481
[`hash.digest()`]: crypto.html#crypto_hash_digest_encoding
14641482
[`hash.update()`]: crypto.html#crypto_hash_update_data_inputencoding
1483+
[`readable._read()`]: stream.html#stream_readable_read_size_1
14651484
[`sign.sign()`]: crypto.html#crypto_sign_sign_privatekey_outputformat
1485+
[`stream.push()`]: stream.html#stream_readable_push_chunk_encoding
1486+
[`stream.unshift()`]: stream.html#stream_readable_unshift_chunk
14661487
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
14671488
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
14681489
[`fs.readFileSync`]: fs.html#fs_fs_readfilesync_path_options

lib/_stream_readable.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const util = require('util');
3131
const debug = util.debuglog('stream');
3232
const BufferList = require('internal/streams/BufferList');
3333
const destroyImpl = require('internal/streams/destroy');
34+
const errors = require('internal/errors');
3435
var StringDecoder;
3536

3637
util.inherits(Readable, Stream);
@@ -233,11 +234,12 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
233234

234235
if (addToFront) {
235236
if (state.endEmitted)
236-
stream.emit('error', new Error('stream.unshift() after end event'));
237+
stream.emit('error',
238+
new errors.Error('ERR_STREAM_UNSHIFT_AFTER_END_EVENT'));
237239
else
238240
addChunk(stream, state, chunk, true);
239241
} else if (state.ended) {
240-
stream.emit('error', new Error('stream.push() after EOF'));
242+
stream.emit('error', new errors.Error('ERR_STREAM_PUSH_AFTER_EOF'));
241243
} else {
242244
state.reading = false;
243245
if (state.decoder && !encoding) {
@@ -548,7 +550,7 @@ function maybeReadMore_(stream, state) {
548550
// for virtual (non-string, non-buffer) streams, "length" is somewhat
549551
// arbitrary, and perhaps not very meaningful.
550552
Readable.prototype._read = function(n) {
551-
this.emit('error', new Error('_read() is not implemented'));
553+
this.emit('error', new errors.Error('ERR_STREAM_READ_NOT_IMPLEMENTED'));
552554
};
553555

554556
Readable.prototype.pipe = function(dest, pipeOpts) {

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ E('ERR_SOCKET_CLOSED', 'Socket is closed');
325325
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
326326
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
327327
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
328+
E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
329+
E('ERR_STREAM_READ_NOT_IMPLEMENTED', '_read() is not implemented');
330+
E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
328331
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
329332
E('ERR_TLS_CERT_ALTNAME_INVALID',
330333
'Hostname/IP does not match certificate\'s altnames: %s');
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const stream = require('stream');
44
const assert = require('assert');
55

66
const readable = new stream.Readable();
77

8-
assert.throws(() => readable.read(), /not implemented/);
8+
assert.throws(() => readable.read(), common.expectsError({
9+
code: 'ERR_STREAM_READ_NOT_IMPLEMENTED',
10+
type: Error,
11+
message: '_read() is not implemented'
12+
}));

test/parallel/test-stream-unshift-read-race.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ r._read = function(n) {
7070
function pushError() {
7171
assert.throws(function() {
7272
r.push(Buffer.allocUnsafe(1));
73-
}, /^Error: stream\.push\(\) after EOF$/);
73+
}, common.expectsError({
74+
code: 'ERR_STREAM_PUSH_AFTER_EOF',
75+
type: Error,
76+
message: 'stream.push() after EOF'
77+
}));
7478
}
7579

7680

@@ -84,7 +88,11 @@ w._write = function(chunk, encoding, cb) {
8488
r.on('end', common.mustCall(function() {
8589
assert.throws(function() {
8690
r.unshift(Buffer.allocUnsafe(1));
87-
}, /^Error: stream\.unshift\(\) after end event$/);
91+
}, common.expectsError({
92+
code: 'ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
93+
type: Error,
94+
message: 'stream.unshift() after end event'
95+
}));
8896
w.end();
8997
}));
9098

0 commit comments

Comments
 (0)