W3cubDocs

/Web APIs

ReadableByteStreamController: error() method

The error() method of the ReadableByteStreamController interface causes any future interactions with the associated stream to error with the specified reason.

This is commonly called by an underlying source to surface an error from the interface where it gets its data (such as a file-read or socket error). It can also be called from elsewhere to trigger a stream error, for example if another part of the system that the stream relies on fails.

Syntax

js

error(errorObject)

Parameters

errorObject

Any object that you want future interactions to fail with.

Return value

None (undefined).

Exceptions

TypeError

Thrown if the source object is not a ReadableByteStreamController, or the stream is not readable for some other reason.

Examples

The example in Using readable byte streams > Creating a readable socket push byte stream shows how you might use error() to manually trigger a stream error if another part of the system it relies on fails.

Specifically, the underlying source start() method calls readRepeatedly() to perform all setup operations and to make a request for data. This returns a promise. If there are any errors thrown when reading the data they will be caught by the chained catch() function. In catch() we then call error() on the controller, passing the reason from the underlying source.

js

function start(controller) {
  readRepeatedly().catch((e) => controller.error(e));
}

function readRepeatedly() {
  return socket.select2().then(() => {
    // …
  });
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
error 89 89 102 No 75 No 89 89 102 63 No 15.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableByteStreamController/error