大网数据广告活动
Web 标准与前端开发

ReadableByteStreamController: error() method

· 52CE 我爱测

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

Streams

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

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.

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

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

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。