@@ -1880,13 +1880,11 @@ or write buffered data before a stream ends.
1880
1880
1881
1881
#### Errors While Writing
1882
1882
1883
- It is recommended that errors occurring during the processing of the
1884
- ` writable._write() ` and ` writable._writev() ` methods are reported by invoking
1885
- the callback and passing the error as the first argument. This will cause an
1886
- ` 'error' ` event to be emitted by the ` Writable ` . Throwing an ` Error ` from within
1887
- ` writable._write() ` can result in unexpected and inconsistent behavior depending
1888
- on how the stream is being used. Using the callback ensures consistent and
1889
- predictable handling of errors.
1883
+ Errors occurring during the processing of the [ ` writable._write() ` ] [ ] ,
1884
+ [ ` writable._writev() ` ] [ ] and [ ` writable._final() ` ] methods must be propagated
1885
+ by invoking the callback and passing the error as the first argument.
1886
+ Throwing an ` Error ` from within these methods or manually emitting an ` 'error' `
1887
+ event results in undefined behavior.
1890
1888
1891
1889
If a ` Readable ` stream pipes into a ` Writable ` stream when ` Writable ` emits an
1892
1890
error, the ` Readable ` stream will be unpiped.
@@ -2160,24 +2158,22 @@ buffer. See [`readable.push('')`][] for more information.
2160
2158
2161
2159
#### Errors While Reading
2162
2160
2163
- It is recommended that errors occurring during the processing of the
2164
- ` readable._read() ` method are emitted using the ` 'error' ` event rather than
2165
- being thrown. Throwing an ` Error ` from within ` readable._read() ` can result in
2166
- unexpected and inconsistent behavior depending on whether the stream is
2167
- operating in flowing or paused mode. Using the ` 'error' ` event ensures
2168
- consistent and predictable handling of errors.
2161
+ Errors occurring during processing of the [ ` readable._read() ` ] [ ] must be
2162
+ propagated through the [ ` readable.destroy(err) ` ] [ readable-_destroy ] method.
2163
+ Throwing an ` Error ` from within [ ` readable._read() ` ] [ ] or manually emitting an
2164
+ ` 'error' ` event results in undefined behavior.
2169
2165
2170
- <!-- eslint-disable no-useless-return -->
2171
2166
``` js
2172
2167
const { Readable } = require (' stream' );
2173
2168
2174
2169
const myReadable = new Readable ({
2175
2170
read (size ) {
2176
- if (checkSomeErrorCondition ()) {
2177
- process .nextTick (() => this .emit (' error' , err));
2178
- return ;
2171
+ const err = checkSomeErrorCondition ();
2172
+ if (err) {
2173
+ this .destroy (err);
2174
+ } else {
2175
+ // Do some work.
2179
2176
}
2180
- // Do some work.
2181
2177
}
2182
2178
});
2183
2179
```
@@ -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