# Error injection

Rate limits, overloads, timeouts, malformed chunks and cut-off streams, on demand.

Real providers fail in ways that are hard to reproduce. A mock fails exactly when you ask it to. There are three kinds of failure.

## HTTP errors [#http-errors]

`error(status, options?)` returns a non-2xx response in the endpoint's real error shape, so SDKs raise their usual classes, like `OpenAI.RateLimitError`, `Anthropic.RateLimitError` and the AI SDK's `APICallError`.

```ts
{ when: { header: { "x-mock-fail": "rate" } }, reply: error(429, { retryAfter: 2 }) }
```

| Call                                     | What the client sees                                    |
| ---------------------------------------- | ------------------------------------------------------- |
| `error(429, { retryAfter })`             | Rate limit with a `retry-after` header                  |
| `error(400, { message })`                | Invalid request                                         |
| `error(401)`, `error(403)`, `error(404)` | Auth and not-found errors                               |
| `error(500)`, `error(503)`               | Server errors                                           |
| `error(529)`                             | Anthropic `overloaded_error`. Other endpoints get a 503 |

| Endpoint    | Error body                                            |
| ----------- | ----------------------------------------------------- |
| OpenAI      | `{ "error": { "message", "type", "param", "code" } }` |
| Anthropic   | `{ "type": "error", "error": { "type", "message" } }` |
| AI SDK, eve | `{ "error": "message" }`                              |

## Stream faults [#stream-faults]

`fault(kind, options?)` breaks the connection itself.

| Call                                  | What happens                                                         |
| ------------------------------------- | -------------------------------------------------------------------- |
| `fault("abort", { afterChunks })`     | The stream starts, then the connection closes without a finish event |
| `fault("timeout", { afterMs })`       | The connection stays open and sends nothing until it closes          |
| `fault("malformed", { afterChunks })` | A chunk that isn't valid JSON                                        |

`afterChunks` lets the stream start normally and fail partway, which is where retry and resume logic usually breaks:

```ts
reply: [text("Here is a long answer that will never finish"), fault("abort", { afterChunks: 3 })];
```

## Finish reasons [#finish-reasons]

Some failures are successful responses that end badly. `finish(reason)` sets the reason using the AI SDK's names, translated for each endpoint:

| Call                       | OpenAI           | Anthropic    | AI SDK           |
| -------------------------- | ---------------- | ------------ | ---------------- |
| `finish("length")`         | `length`         | `max_tokens` | `length`         |
| `finish("content-filter")` | `content_filter` | `refusal`    | `content-filter` |

```ts
reply: [text("The answer is cut off right"), finish("length")];
```

## Flaky on purpose [#flaky-on-purpose]

`oneOf` fails a share of requests, seeded so it fails the same requests on every run:

```ts
reply: oneOf([
  { weight: 9, reply: text("OK") },
  { weight: 1, reply: error(503) },
]);
```