WritableStreamDefaultController: error() メソッド

Baseline 2022

Newly available

Since June 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

WritableStreamDefaultController インターフェイスの error() メソッドにより、関連するストリームとの今後のやり取りがエラーになります。

通常、基になるシンクのメソッドの 1 つから拒否されたプロミスを返すだけで十分なため、このメソッドはほとんど使用されません。 ただし、基になるシンクとのやり取りの通常のライフサイクル外のイベントに応答して、ストリームを突然シャットダウンする場合に役立ちます。

構文

js
error(message)

引数

message

文字列で、将来の操作を失敗するようにしたいエラーを表します。

返値

なし (undefined)。

例外

TypeError

エラーにしようとしているストリームは WritableStream ではありません。

js
const writableStream = new WritableStream({
  start(controller) {
    // コントローラーで何かをする

    // 必要に応じてストリームをエラーにする
    controller.error("My error is broken");
  },
  write(chunk, controller) {
    // ...
  },
  close(controller) {
    // ...
  },
  abort(err) {
    // ...
  },
});

仕様書

Specification
Streams Standard
# ref-for-ws-default-controller-error①

ブラウザーの互換性

BCD tables only load in the browser