> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bridgly.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Errors

> Handle API and network failures predictably.

## The result union

No SDK method throws for API or network errors. Every scraping method returns an
`ApiResult`, a discriminated union you narrow on the `type` field:

```ts theme={"dark"}
type ApiResult<T> =
  | { type: 'success'; data: T; timeMs: number; creditsCost: number }
  | { type: 'error'; status: number; message: string };
```

Checking `type` narrows the rest of the object — in the `success` branch
`data`, `timeMs`, and `creditsCost` are all available and typed; in the `error`
branch you get `status` and `message`.

```ts theme={"dark"}
const res = await client.x.getUser({ username: 'jack' });

if (res.type === 'error') {
  // handle and return early
  console.error(`Bridgly error ${res.status}: ${res.message}`);
  return;
}

// res.data is fully typed from here on
console.log(res.data);
```

## Error status codes

The `status` field carries the HTTP status code. Branch on it rather than
parsing the message string.

| Status | Meaning                                      | What to do                                                     |
| ------ | -------------------------------------------- | -------------------------------------------------------------- |
| `400`  | The request failed validation.               | Check the field values against the endpoint schema.            |
| `401`  | Missing, malformed, or revoked API key.      | Verify the key you passed to `new Bridgly(...)`.               |
| `402`  | Out of credits.                              | Top up from the dashboard or with `client.billing.buyCredits`. |
| `404`  | The requested resource wasn't found.         | Confirm the identifier exists and is public.                   |
| `500`  | The scrape failed unexpectedly.              | Retry after a short delay.                                     |
| `0`    | Network error — the request never completed. | Check connectivity; the `message` has the detail.              |
