@@ -1879,13 +1879,11 @@ or write buffered data before a stream ends.
1879
1879
1880
1880
#### Errors While Writing
1881
1881
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.
1889
1887
1890
1888
If a ` Readable ` stream pipes into a ` Writable ` stream when ` Writable ` emits an
1891
1889
error, the ` Readable ` stream will be unpiped.
@@ -2159,24 +2157,22 @@ buffer. See [`readable.push('')`][] for more information.
2159
2157
2160
2158
#### Errors While Reading
2161
2159
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.
2168
2164
2169
- <!-- eslint-disable no-useless-return -->
2170
2165
``` js
2171
2166
const { Readable } = require (' stream' );
2172
2167
2173
2168
const myReadable = new Readable ({
2174
2169
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.
2178
2175
}
2179
- // Do some work.
2180
2176
}
2181
2177
});
2182
2178
```
@@ -2776,6 +2772,7 @@ contain multi-byte characters.
2776
2772
[ `process.stderr` ] : process.html#process_process_stderr
2777
2773
[ `process.stdin` ] : process.html#process_process_stdin
2778
2774
[ `process.stdout` ] : process.html#process_process_stdout
2775
+ [ `readable._read()` ] : #stream_readable_read_size_1
2779
2776
[ `readable.push('')` ] : #stream_readable_push
2780
2777
[ `readable.setEncoding()` ] : #stream_readable_setencoding_encoding
2781
2778
[ `stream.Readable.from()` ] : #stream_stream_readable_from_iterable_options
@@ -2786,6 +2783,9 @@ contain multi-byte characters.
2786
2783
[ `stream.uncork()` ] : #stream_writable_uncork
2787
2784
[ `stream.unpipe()` ] : #stream_readable_unpipe_destination
2788
2785
[ `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
2789
2789
[ `writable.cork()` ] : #stream_writable_cork
2790
2790
[ `writable.end()` ] : #stream_writable_end_chunk_encoding_callback
2791
2791
[ `writable.uncork()` ] : #stream_writable_uncork
0 commit comments