Skip to content

Commit b59c513

Browse files
ronagdanielleadams
authored andcommitted
stream: add isErrored helper
Refs: nodejs/undici#1134 PR-URL: #41121 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 35fe144 commit b59c513

File tree

7 files changed

+64
-7
lines changed

7 files changed

+64
-7
lines changed

doc/api/stream.md

+13
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,19 @@ added: v16.8.0
21712171

21722172
Returns whether the stream has been read from or cancelled.
21732173

2174+
### `stream.isErrored(stream)`
2175+
2176+
<!-- YAML
2177+
added: REPLACEME
2178+
-->
2179+
2180+
> Stability: 1 - Experimental
2181+
2182+
* `stream` {Readable|Writable|Duplex|WritableStream|ReadableStream}
2183+
* Returns: {boolean}
2184+
2185+
Returns whether the stream has encountered an error.
2186+
21742187
### `stream.Readable.toWeb(streamReadable)`
21752188

21762189
<!-- YAML

lib/internal/streams/utils.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
} = primordials;
88

99
const kDestroyed = Symbol('kDestroyed');
10+
const kIsErrored = Symbol('kIsErrored');
1011
const kIsDisturbed = Symbol('kIsDisturbed');
1112

1213
function isReadableNodeStream(obj, strict = false) {
@@ -211,16 +212,29 @@ function willEmitClose(stream) {
211212

212213
function isDisturbed(stream) {
213214
return !!(stream && (
214-
stream.readableDidRead ||
215-
stream.readableAborted ||
216-
stream[kIsDisturbed]
215+
stream[kIsDisturbed] ??
216+
(stream.readableDidRead || stream.readableAborted)
217+
));
218+
}
219+
220+
function isErrored(stream) {
221+
return !!(stream && (
222+
stream[kIsErrored] ??
223+
stream.readableErrored ??
224+
stream.writableErrored ??
225+
stream._readableState?.errorEmitted ??
226+
stream._writableState?.errorEmitted ??
227+
stream._readableState?.errored ??
228+
stream._writableState?.errored
217229
));
218230
}
219231

220232
module.exports = {
221233
kDestroyed,
222234
isDisturbed,
235+
isErrored,
223236
kIsDisturbed,
237+
kIsErrored,
224238
isClosed,
225239
isDestroyed,
226240
isDuplexNodeStream,

lib/internal/webstreams/readablestream.js

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const {
8282

8383
const {
8484
kIsDisturbed,
85+
kIsErrored,
8586
} = require('internal/streams/utils');
8687

8788
const {
@@ -241,6 +242,10 @@ class ReadableStream {
241242
return this[kState].disturbed;
242243
}
243244

245+
get [kIsErrored]() {
246+
return this[kState].state === 'errored';
247+
}
248+
244249
/**
245250
* @readonly
246251
* @type {boolean}

lib/stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ const eos = require('internal/streams/end-of-stream');
3636
const internalBuffer = require('internal/buffer');
3737

3838
const promises = require('stream/promises');
39+
const utils = require('internal/streams/utils');
3940

4041
const Stream = module.exports = require('internal/streams/legacy').Stream;
41-
Stream.isDisturbed = require('internal/streams/utils').isDisturbed;
42+
Stream.isDisturbed = utils.isDisturbed;
43+
Stream.isErrored = utils.isErrored;
4244
Stream.Readable = require('internal/streams/readable');
4345
Stream.Writable = require('internal/streams/writable');
4446
Stream.Duplex = require('internal/streams/duplex');

test/parallel/test-stream-readable-didRead.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const { isDisturbed, Readable } = require('stream');
4+
const { isDisturbed, isErrored, Readable } = require('stream');
55

66
function noop() {}
77

88
function check(readable, data, fn) {
99
assert.strictEqual(readable.readableDidRead, false);
1010
assert.strictEqual(isDisturbed(readable), false);
11+
assert.strictEqual(isErrored(readable), false);
1112
if (data === -1) {
12-
readable.on('error', common.mustCall());
13+
readable.on('error', common.mustCall(() => {
14+
assert.strictEqual(isErrored(readable), true);
15+
}));
1316
readable.on('data', common.mustNotCall());
1417
readable.on('end', common.mustNotCall());
1518
} else {

test/parallel/test-whatwg-readablestream.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
const common = require('../common');
5-
const { isDisturbed } = require('stream');
5+
const { isDisturbed, isErrored } = require('stream');
66
const assert = require('assert');
77
const {
88
isPromise,
@@ -1572,3 +1572,19 @@ class Source {
15721572
isDisturbed(stream, true);
15731573
})().then(common.mustCall());
15741574
}
1575+
1576+
1577+
{
1578+
const stream = new ReadableStream({
1579+
pull: common.mustCall((controller) => {
1580+
controller.error(new Error());
1581+
}),
1582+
});
1583+
1584+
const reader = stream.getReader();
1585+
(async () => {
1586+
isErrored(stream, false);
1587+
await reader.read().catch(common.mustCall());
1588+
isErrored(stream, true);
1589+
})().then(common.mustCall());
1590+
}

tools/doc/type-parser.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ const customTypesMap = {
211211
'stream.Readable': 'stream.html#class-streamreadable',
212212
'stream.Transform': 'stream.html#class-streamtransform',
213213
'stream.Writable': 'stream.html#class-streamwritable',
214+
'Duplex': 'stream.html#class-streamduplex',
215+
'Readable': 'stream.html#class-streamreadable',
216+
'Transform': 'stream.html#class-streamtransform',
217+
'Writable': 'stream.html#class-streamwritable',
214218

215219
'Immediate': 'timers.html#class-immediate',
216220
'Timeout': 'timers.html#class-timeout',

0 commit comments

Comments
 (0)