Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate _stream_readable errors to internal/errors #15042

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
stream: migrate _stream_readable use error codes
  • Loading branch information
benhalverson authored and joyeecheung committed Oct 28, 2017
commit b635d1aa2b3d4b6c3bbfa9fe0d3e413460405dbe
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
5 changes: 5 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ 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_ENDREADABLE_CALLED_ON_NONEMPTY',
'endReadable() called on a non-empty stream');
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,11 @@
'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({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a suggestion - it would be nicer to use

common.expectsError(throwingFn, errorObj)
// instead of
assert.throws(throwingFn, common.expectsError(errorObj))

type: Error,
message: 'ERR_STREAM_READ_NOT_IMPLEMENTED'
}));
5 changes: 4 additions & 1 deletion test/parallel/test-stream-unshift-read-race.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ r._read = function(n) {
function pushError() {
assert.throws(function() {
r.push(Buffer.allocUnsafe(1));
}, /^Error: stream\.push\(\) after EOF$/);
}, common.expectsError({
type: Error,
message: 'stream.push() after EOF'
}));
}


Expand Down