Skip to content

Commit 2651f29

Browse files
committed
stream: complete migration to internal/errors
Complete the migration to the new error system of _stream_readable and _stream_writable. Adds the corresponding documentation.
1 parent 88fb359 commit 2651f29

8 files changed

+77
-25
lines changed

doc/api/errors.md

+20
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,18 @@ 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_CANNOT_PIPE"></a>
1321+
### ERR_STREAM_CANNOT_PIPE
1322+
1323+
Used when an attempt is made to call [`stream.pipe()`][] on a
1324+
[`Writable`][] stream.
1325+
1326+
<a id="ERR_STREAM_NULL_VALUES"></a>
1327+
### ERR_STREAM_NULL_VALUES
1328+
1329+
Used when an attempt is made to call [`stream.write()`][] with a `null`
1330+
chunk.
1331+
13201332
<a id="ERR_STREAM_PUSH_AFTER_EOF"></a>
13211333
### ERR_STREAM_PUSH_AFTER_EOF
13221334

@@ -1349,6 +1361,12 @@ const instance = new Socket();
13491361
instance.setEncoding('utf8');
13501362
```
13511363

1364+
<a id="ERR_STREAM_WRITE_AFTER_END"></a>
1365+
### ERR_STREAM_WRITE_AFTER_END
1366+
1367+
Used when an attempt is made to call [`stream.write()`][] after
1368+
`stream.end()` has been called.
1369+
13521370
<a id="ERR_TLS_CERT_ALTNAME_INVALID"></a>
13531371
### ERR_TLS_CERT_ALTNAME_INVALID
13541372

@@ -1484,6 +1502,8 @@ closed.
14841502
[`sign.sign()`]: crypto.html#crypto_sign_sign_privatekey_outputformat
14851503
[`stream.push()`]: stream.html#stream_readable_push_chunk_encoding
14861504
[`stream.unshift()`]: stream.html#stream_readable_unshift_chunk
1505+
[`stream.write()`]: stream.html#stream_writable_write_chunk_encoding_callback
1506+
[`Writable`]: stream.html#stream_class_stream_writable
14871507
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
14881508
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
14891509
[`fs.readFileSync`]: fs.html#fs_fs_readfilesync_path_options

lib/_stream_readable.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ function chunkInvalid(state, chunk) {
284284
typeof chunk !== 'string' &&
285285
chunk !== undefined &&
286286
!state.objectMode) {
287-
er = new TypeError('Invalid non-string/buffer chunk');
287+
er = new errors.TypeError('ERR_INVALID_ARG_TYPE',
288+
'chunk', 'string/Buffer/Uint8Array');
288289
}
289290
return er;
290291
}

lib/_stream_writable.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ function Writable(options) {
229229

230230
// Otherwise people can pipe Writable streams, which is just wrong.
231231
Writable.prototype.pipe = function() {
232-
this.emit('error', new Error('Cannot pipe, not readable'));
232+
this.emit('error', new errors.Error('ERR_STREAM_CANNOT_PIPE'));
233233
};
234234

235235

236236
function writeAfterEnd(stream, cb) {
237-
var er = new Error('write after end');
237+
var er = new errors.Error('ERR_STREAM_WRITE_AFTER_END');
238238
// TODO: defer error events consistently everywhere, not just the cb
239239
stream.emit('error', er);
240240
process.nextTick(cb, er);
@@ -248,11 +248,11 @@ function validChunk(stream, state, chunk, cb) {
248248
var er = false;
249249

250250
if (chunk === null) {
251-
er = new TypeError('May not write null values to stream');
251+
er = new errors.TypeError('ERR_STREAM_NULL_VALUES');
252252
} else if (typeof chunk !== 'string' &&
253253
chunk !== undefined &&
254254
!state.objectMode) {
255-
er = new TypeError('Invalid non-string/buffer chunk');
255+
er = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'chunk', 'string/buffer');
256256
}
257257
if (er) {
258258
stream.emit('error', er);
@@ -533,7 +533,7 @@ function clearBuffer(stream, state) {
533533
}
534534

535535
Writable.prototype._write = function(chunk, encoding, cb) {
536-
cb(new Error('_write() is not implemented'));
536+
cb(new errors.Error('ERR_METHOD_NOT_IMPLEMENTED', '_transform'));
537537
};
538538

539539
Writable.prototype._writev = null;

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,13 @@ 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_CANNOT_PIPE', 'Cannot pipe, not readable');
329+
E('ERR_STREAM_NULL_VALUES', 'May not write null values to stream');
328330
E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
329331
E('ERR_STREAM_READ_NOT_IMPLEMENTED', '_read() is not implemented');
330332
E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
331333
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
334+
E('ERR_STREAM_WRITE_AFTER_END', 'write after end');
332335
E('ERR_TLS_CERT_ALTNAME_INVALID',
333336
'Hostname/IP does not match certificate\'s altnames: %s');
334337
E('ERR_TLS_DH_PARAM_SIZE', (size) =>

test/parallel/test-file-write-stream.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ file
6464
assert.strictEqual(file.bytesWritten, EXPECTED.length * 2);
6565

6666
callbacks.close++;
67-
assert.throws(function() {
68-
console.error('write after end should not be allowed');
69-
file.write('should not work anymore');
70-
}, /^Error: write after end$/);
67+
common.expectsError(
68+
() => {
69+
console.error('write after end should not be allowed');
70+
file.write('should not work anymore');
71+
},
72+
{
73+
code: 'ERR_STREAM_WRITE_AFTER_END',
74+
type: Error,
75+
message: 'write after end'
76+
}
77+
);
7178

7279
fs.unlinkSync(fn);
7380
});

test/parallel/test-http2-head-request.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const http2 = require('http2');
88

99
const errCheck = common.expectsError({
1010
type: Error,
11+
code: 'ERR_STREAM_WRITE_AFTER_END',
1112
message: 'write after end'
1213
}, 2);
1314

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const stream = require('stream');
5-
const assert = require('assert');
65

76
const readable = new stream.Readable({
87
read: () => {}
98
});
109

11-
const errMessage = /Invalid non-string\/buffer chunk/;
12-
assert.throws(() => readable.push([]), errMessage);
13-
assert.throws(() => readable.push({}), errMessage);
14-
assert.throws(() => readable.push(0), errMessage);
10+
function checkError(fn) {
11+
common.expectsError(fn, {
12+
code: 'ERR_INVALID_ARG_TYPE',
13+
type: TypeError
14+
});
15+
}
16+
17+
checkError(() => readable.push([]));
18+
checkError(() => readable.push({}));
19+
checkError(() => readable.push(0));

test/parallel/test-stream-writable-null.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44

55
const stream = require('stream');
@@ -16,10 +16,18 @@ MyWritable.prototype._write = function(chunk, encoding, callback) {
1616
callback();
1717
};
1818

19-
assert.throws(() => {
20-
const m = new MyWritable({ objectMode: true });
21-
m.write(null, (err) => assert.ok(err));
22-
}, /^TypeError: May not write null values to stream$/);
19+
common.expectsError(
20+
() => {
21+
const m = new MyWritable({ objectMode: true });
22+
m.write(null, (err) => assert.ok(err));
23+
},
24+
{
25+
code: 'ERR_STREAM_NULL_VALUES',
26+
type: TypeError,
27+
message: 'May not write null values to stream'
28+
}
29+
);
30+
2331
assert.doesNotThrow(() => {
2432
const m = new MyWritable({ objectMode: true }).on('error', (e) => {
2533
assert.ok(e);
@@ -29,10 +37,17 @@ assert.doesNotThrow(() => {
2937
});
3038
});
3139

32-
assert.throws(() => {
33-
const m = new MyWritable();
34-
m.write(false, (err) => assert.ok(err));
35-
}, /^TypeError: Invalid non-string\/buffer chunk$/);
40+
common.expectsError(
41+
() => {
42+
const m = new MyWritable();
43+
m.write(false, (err) => assert.ok(err));
44+
},
45+
{
46+
code: 'ERR_INVALID_ARG_TYPE',
47+
type: TypeError
48+
}
49+
);
50+
3651
assert.doesNotThrow(() => {
3752
const m = new MyWritable().on('error', (e) => {
3853
assert.ok(e);

0 commit comments

Comments
 (0)