Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d258e02

Browse filesBrowse files
ronagtargos
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 0fc85ff commit d258e02
Copy full SHA for d258e02

File tree

Expand file treeCollapse file tree

1 file changed

+18
-18
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-18
lines changed
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+18-18Lines changed: 18 additions & 18 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1880,13 +1880,11 @@ or write buffered data before a stream ends.
18801880

18811881
#### Errors While Writing
18821882

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.
18901888

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

21612159
#### Errors While Reading
21622160

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.
21692165

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

21742169
const myReadable = new Readable({
21752170
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.
21792176
}
2180-
// Do some work.
21812177
}
21822178
});
21832179
```
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.