Skip to content

Commit 7223ce2

Browse files
ronagTrott
authored andcommitted
doc: clarify stream errors while reading and writing
Errors should be propagated through destroy(err). Anything else is basically undefined behaviour. PR-URL: #29653 Refs: #29584 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent ccb524f commit 7223ce2

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

doc/api/stream.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,13 +1879,11 @@ or write buffered data before a stream ends.
18791879

18801880
#### Errors While Writing
18811881

1882-
It is recommended that errors occurring during the processing of the
1883-
`writable._write()` and `writable._writev()` methods are reported by invoking
1884-
the callback and passing the error as the first argument. This will cause an
1885-
`'error'` event to be emitted by the `Writable`. Throwing an `Error` from within
1886-
`writable._write()` can result in unexpected and inconsistent behavior depending
1887-
on how the stream is being used. Using the callback ensures consistent and
1888-
predictable handling of errors.
1882+
Errors occurring during the processing of the [`writable._write()`][],
1883+
[`writable._writev()`][] and [`writable._final()`] methods must be propagated
1884+
by invoking the callback and passing the error as the first argument.
1885+
Throwing an `Error` from within these methods or manually emitting an `'error'`
1886+
event results in undefined behavior.
18891887

18901888
If a `Readable` stream pipes into a `Writable` stream when `Writable` emits an
18911889
error, the `Readable` stream will be unpiped.
@@ -2159,24 +2157,22 @@ buffer. See [`readable.push('')`][] for more information.
21592157

21602158
#### Errors While Reading
21612159

2162-
It is recommended that errors occurring during the processing of the
2163-
`readable._read()` method are emitted using the `'error'` event rather than
2164-
being thrown. Throwing an `Error` from within `readable._read()` can result in
2165-
unexpected and inconsistent behavior depending on whether the stream is
2166-
operating in flowing or paused mode. Using the `'error'` event ensures
2167-
consistent and predictable handling of errors.
2160+
Errors occurring during processing of the [`readable._read()`][] must be
2161+
propagated through the [`readable.destroy(err)`][readable-_destroy] method.
2162+
Throwing an `Error` from within [`readable._read()`][] or manually emitting an
2163+
`'error'` event results in undefined behavior.
21682164

2169-
<!-- eslint-disable no-useless-return -->
21702165
```js
21712166
const { Readable } = require('stream');
21722167

21732168
const myReadable = new Readable({
21742169
read(size) {
2175-
if (checkSomeErrorCondition()) {
2176-
process.nextTick(() => this.emit('error', err));
2177-
return;
2170+
const err = checkSomeErrorCondition();
2171+
if (err) {
2172+
this.destroy(err);
2173+
} else {
2174+
// Do some work.
21782175
}
2179-
// Do some work.
21802176
}
21812177
});
21822178
```
@@ -2776,6 +2772,7 @@ contain multi-byte characters.
27762772
[`process.stderr`]: process.html#process_process_stderr
27772773
[`process.stdin`]: process.html#process_process_stdin
27782774
[`process.stdout`]: process.html#process_process_stdout
2775+
[`readable._read()`]: #stream_readable_read_size_1
27792776
[`readable.push('')`]: #stream_readable_push
27802777
[`readable.setEncoding()`]: #stream_readable_setencoding_encoding
27812778
[`stream.Readable.from()`]: #stream_stream_readable_from_iterable_options
@@ -2786,6 +2783,9 @@ contain multi-byte characters.
27862783
[`stream.uncork()`]: #stream_writable_uncork
27872784
[`stream.unpipe()`]: #stream_readable_unpipe_destination
27882785
[`stream.wrap()`]: #stream_readable_wrap_stream
2786+
[`writable._final()`]: #stream_writable_final_callback
2787+
[`writable._write()`]: #stream_writable_write_chunk_encoding_callback_1
2788+
[`writable._writev()`]: #stream_writable_writev_chunks_callback
27892789
[`writable.cork()`]: #stream_writable_cork
27902790
[`writable.end()`]: #stream_writable_end_chunk_encoding_callback
27912791
[`writable.uncork()`]: #stream_writable_uncork

0 commit comments

Comments
 (0)