> For the complete documentation index, see [llms.txt](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/sdk/errors.md).

# Error handling

The SDK throws `BlackSheepError` for any non-2xx response. The browser `fetch` errors (network down, DNS, TLS) bubble up as the underlying `TypeError`.

```ts
import { BlackSheep, BlackSheepError } from "@blacksheep/sdk";

try {
  await bs.agents.send(id, { to, amount });
} catch (e) {
  if (e instanceof BlackSheepError) {
    // server-side rejection
    handleApiError(e.status, e.message);
  } else {
    // network failure, retry with backoff
    throw e;
  }
}
```

## Status codes

| Status      | Meaning                            | Retry?                     |
| ----------- | ---------------------------------- | -------------------------- |
| 400         | Invalid input                      | No                         |
| 401         | Bad / missing API key              | No                         |
| 403         | Blocked by policy                  | No                         |
| 404         | Resource not found                 | No                         |
| 409         | Conflict (e.g. insufficient funds) | After fixing               |
| 429         | Rate limited                       | Yes, with backoff          |
| 500         | Server error                       | Yes, idempotent calls only |
| 502/503/504 | Upstream / gateway                 | Yes, with backoff          |

## Retry strategy

For idempotent reads (`agents.list`, `agents.get`, `transactions.list`):

```ts
async function withRetry<T>(fn: () => Promise<T>, attempts = 4): Promise<T> {
  for (let i = 0; i < attempts; i++) {
    try { return await fn(); }
    catch (e) {
      if (i === attempts - 1) throw e;
      if (e instanceof BlackSheepError && [429, 500, 502, 503, 504].includes(e.status)) {
        await new Promise(r => setTimeout(r, 200 * 2 ** i));
      } else throw e;
    }
  }
  throw new Error("unreachable");
}
```

Do **not** auto-retry `send` — it isn't idempotent yet.

## Common messages

| Message                     | Likely cause                                     |
| --------------------------- | ------------------------------------------------ |
| `Unauthorized`              | Missing or wrong `Authorization` header          |
| `Blocked by policy`         | One of the policy rules denied the spend         |
| `Wallet not found`          | Wrong agent ID, or key not scoped to that wallet |
| `Insufficient USDC`         | Top-up the wallet                                |
| `Insufficient SOL for fee`  | Send a fraction of SOL to the wallet for gas     |
| `Invalid recipient address` | Not valid base58 / wrong length                  |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/sdk/errors.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
