---
title: "error(reason) | Grafana k6 documentation"
description: "The error method of the ReadableStreamDefaultController makes any future interactions with the associated stream to error."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# error(reason)

The `error()` method of the [ReadableStreamDefaultController](/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/) makes any future interactions with the associated stream to error.

## Parameters

Expand table

| Name | Type | Description                                                     |
|------|------|-----------------------------------------------------------------|
| e    | any  | The error to return to any future interactions with the stream. |

## Exceptions

Expand table

| Exception | Description                                                                                                                                                        |
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| TypeError | Thrown when the source object is not a [ReadableStreamDefaultController](/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/). |

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import { ReadableStream } from 'k6/experimental/streams';
import { setTimeout } from 'k6/timers';

export default async function () {
  let currentNumber = 0;

  const stream = new ReadableStream({
    start(controller) {
      const fn = () => {
        if (currentNumber % 8 == 0) {
          // Error the stream when the number is divisible by 8
          controller.error('erroring the stream because we received a multiple of 8');
          return;
        }

        if (currentNumber < 10) {
          controller.enqueue(++currentNumber);
          setTimeout(fn, 1000);
          return;
        }

        controller.close();
      };
      setTimeout(fn, 1000);
    },
  });

  const reader = stream.getReader();

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    console.log(`received number ${value} from stream`);
  }
}
```
