Skip to content

Commit f6d7882

Browse files
committed
Tidy up precondition checking in ErrorReadableStream
Following the established pattern, public API methods should throw if preconditions are violated, whereas abstract operations should assert that they are not.
1 parent 329f696 commit f6d7882

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

index.bs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ Instances of <code>ReadableStreamController</code> are created with the internal
559559

560560
<pre is="emu-alg">
561561
1. If IsReadableStreamController(*this*) is *false*, throw a *TypeError* exception.
562+
1. If *this*@[[controlledReadableStream]]@[[state]] is not "readable", throw a *TypeError* exception.
562563
1. Return ErrorReadableStream(*this*@[[controlledReadableStream]]).
563564
</pre>
564565

@@ -841,8 +842,9 @@ Instances of <code>ReadableStreamReader</code> are created with the internal slo
841842

842843
<h4 id="error-readable-stream" aoid="ErrorReadableStream">ErrorReadableStream ( stream, e )</h4>
843844

845+
844846
<pre is="emu-alg">
845-
1. If _stream_@[[state]] is not "readable", throw a *TypeError* exception.
847+
1. Assert: _stream_@[[state]] is "readable".
846848
1. Let _stream_@[[queue]] be a new empty List.
847849
1. Set _stream_@[[storedError]] to _e_.
848850
1. Set _stream_@[[state]] to "errored".

reference-implementation/lib/readable-stream.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ class ReadableStreamController {
198198
throw new TypeError('ReadableStreamController.prototype.error can only be used on a ReadableStreamController');
199199
}
200200

201+
if (this._controlledReadableStream._state !== 'readable') {
202+
throw new TypeError(`The stream is ${this._controlledReadableStream._state} and so cannot be errored`);
203+
}
204+
201205
return ErrorReadableStream(this._controlledReadableStream, e);
202206
}
203207
}
@@ -425,9 +429,7 @@ function EnqueueInReadableStream(stream, chunk) {
425429
}
426430

427431
function ErrorReadableStream(stream, e) {
428-
if (stream._state !== 'readable') {
429-
throw new TypeError(`The stream is ${stream._state} and so cannot be errored`);
430-
}
432+
assert(stream._state === 'readable');
431433

432434
stream._queue = [];
433435
stream._storedError = e;

0 commit comments

Comments
 (0)