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

# Errors

> Every error class that the SDK exports, when each one is thrown, its fields, how to handle it, and how HTTP failures map to errors.

After you read this page, you can catch each SDK error by its class, read its fields, and show the right message.

Every class below extends `Error` and sets `name` to the class name. Use `instanceof` with the class that you import from `@anafi-cc/anafi-sdk`. A check failure, a wrong reply shape, or a refused input that has no class throws a plain `Error` whose message says what failed; each method entry lists them.

## Class tree

```text theme={null}
Error
├── BackendRequestError
│   └── DslStageRefusedError
│       └── NotificationEmailNotVerifiedError
├── DslStaleParentError
├── Erc20AllowNotLiveError
├── Erc20AllowSpentTokenUnnamedError
├── Erc20AllowNativeSwapTokenError
├── Erc20AllowSwapsNotServedError
├── Erc20AllowSwapSupportUnreadError
├── Erc20AllowStagedDocumentMismatchError
├── OwnerRequestCancelledError
└── GenerateDslError
```

Catch the most specific class first.

```ts theme={null}
import {
  BackendRequestError,
  DslStageRefusedError,
  DslStaleParentError,
  NotificationEmailNotVerifiedError,
} from "@anafi-cc/anafi-sdk";

try {
  await anafi.stageDsl(params);
} catch (error) {
  if (error instanceof DslStaleParentError) rebuildOn(error.current);
  else if (error instanceof NotificationEmailNotVerifiedError) askToVerifyInbox();
  else if (error instanceof DslStageRefusedError) showToAuthor(error.refusal);
  else if (error instanceof BackendRequestError && error.status === null) showOffline();
  else throw error;
}
```

## `BackendRequestError`

```ts theme={null}
class BackendRequestError extends Error {
  readonly route: string;           // "<METHOD> <url>"
  readonly status: number | null;   // the HTTP status, or null when no answer arrived
  readonly responseText: string;    // the body, or "" when there was none
  constructor(params: { route: string; status: number | null; responseText: string; message?: string; cause?: unknown });
}
```

A request to the Anafi API that did not succeed. `status` is `null` when the connection failed or the request timed out; `cause` then holds the failure. The default message is `"<route> failed: <status> <body>"`.

| `status` | What to do |
| - | - |
| `null` | The Anafi API did not answer. Retry later. For a revoke of a Safe, `disableSafeModule` already falls back to the owner-sent way. |
| `400` | The request is malformed. Check the input. |
| `403`, `422` | The Anafi API refused the request. Show `responseText`. |
| `5xx` | The Anafi API or the engine failed. Retry later. |

For the error bodies of each route, see [API errors](https://docs.ana.fi/api-reference/errors.md).

## `DslStageRefusedError`

```ts theme={null}
class DslStageRefusedError extends BackendRequestError {
  readonly refusal: string; // the reason of the engine, exactly as it wrote it
}
```

The engine refused to stage a document, and the sealed reason opened. The message is `"the engine refused the document: <refusal>"`. `status` is the HTTP status of the refusal, normally `422`.

Thrown by `stageDsl`, `armSafe`, `commitDslFromSafe` and `stageErc20AllowDsl`. Show `refusal` to the author of the document, fix the document, and stage again.

## `NotificationEmailNotVerifiedError`

```ts theme={null}
class NotificationEmailNotVerifiedError extends DslStageRefusedError {}
```

The engine refused a stage because the document names a `global.notification_email` that is not the verified inbox of the account. Verify the inbox ([notification email](https://docs.ana.fi/sdk/notification-email.md)), or remove the field and the `notify` actions, then stage again.

## `DslStaleParentError`

```ts theme={null}
class DslStaleParentError extends Error {
  readonly current: OnChainDsl | null; // the identity that the chain holds now; null when nothing is committed
}
```

The engine refused to attest because the registry no longer holds the `parent` that you built the document on. Another commit landed since you read it. The message is `"the engine refused the document: <reason>"`. The HTTP status was `409`.

It does not extend `BackendRequestError`. Read the document at `current` (for example with `recover`), apply your change to it, and stage again with `parent: current`.

## `Erc20AllowNotLiveError`

```ts theme={null}
class Erc20AllowNotLiveError extends Error {
  readonly reason: Erc20AllowNotLiveReason;
}

type Erc20AllowNotLiveReason =
  | { kind: "blocked" }
  | { kind: "allowance_zero"; token: Hex }
  | { kind: "cap_exhausted"; token: Hex };
```

The permission of an ERC20Allow account cannot serve the document, and a commit cannot fix it. Thrown by `stageErc20AllowDsl` and the subscription setup.

| `reason.kind` | Message | What to do |
| - | - | - |
| `blocked` | `the owner has blocked the ERC20Allow adapter` | `setErc20AllowBlocked({ blocked: false })`. |
| `allowance_zero` | `token <token> has no allowance to the ERC20Allow adapter` | `approveErc20AllowTokens`. The stage counts an allowance of 0 as approved by the commit, so the stage does not throw this. |
| `cap_exhausted` | `the cap of token <token> has no room left in its window` | Raise the cap, pass `plannedCap`, clear the cap, or wait for the next window. |

## `Erc20AllowSpentTokenUnnamedError`

```ts theme={null}
class Erc20AllowSpentTokenUnnamedError extends Error {
  readonly rule: string | null; // null when the document cannot be read
}
```

A send or a swap input of the document names no token that the SDK can read, or the document cannot be read. A `trigger.value` under a trigger with no token range names no token. Write a literal amount, or give the trigger a token range.

## `Erc20AllowNativeSwapTokenError`

```ts theme={null}
class Erc20AllowNativeSwapTokenError extends Error {
  readonly rule: string;
  readonly side: "sold" | "bought";
  readonly token: RoutingToken; // the native coin
}
```

A swap of `rule` sells or buys the native coin, which the ERC20Allow adapter cannot pull and does not pay out. Use an ERC-20 on that side.

## `Erc20AllowSwapsNotServedError`

```ts theme={null}
class Erc20AllowSwapsNotServedError extends Error {
  readonly rule: string;
  readonly adapter: Hex;
}
```

`rule` swaps, and the ERC20Allow adapter answers no swap executor (the zero address, or a revert). The engine cannot run the swap. Remove the swap from the document.

## `Erc20AllowSwapSupportUnreadError`

```ts theme={null}
class Erc20AllowSwapSupportUnreadError extends Error {
  readonly adapter: Hex;
  // cause: the failure of the read
}
```

The read of `swapExecutor()` failed for a reason other than a revert, so the SDK does not know whether the adapter runs swaps. Retry.

## `Erc20AllowStagedDocumentMismatchError`

```ts theme={null}
class Erc20AllowStagedDocumentMismatchError extends Error {
  readonly field: "commitment" | "tokens";
}
```

A staged value does not match its own text: `commitment` is not `keccak256(salt ‖ yaml)`, or `tokens` is not tokens(D) of `yaml` in order. This happens when the staged value was changed or came from somewhere else. Stage the document again.

## `OwnerRequestCancelledError`

```ts theme={null}
class OwnerRequestCancelledError extends Error {
  constructor();
}
```

Your wallet client throws it when it sees the `signal` of a payment method abort before it asks the wallet. No request reached the wallet. The payment methods catch it and answer `stopped_before_request` with reason `cancelled`. You throw this class; the SDK does not throw it to you.

```ts theme={null}
const walletClient: AnafiWalletClient = {
  // ...
  sendTransaction: async (args, options) => {
    await prepare();
    if (options?.signal?.aborted) throw new OwnerRequestCancelledError();
    return wallet.sendTransaction(args);
  },
};
```

## `GenerateDslError`

```ts theme={null}
type GenerateDslFailure = "unavailable" | "signIn" | "failed";

class GenerateDslError extends Error {
  readonly failure: GenerateDslFailure;
  readonly reason?: string; // the sentence of the Anafi API, when it sent one
  constructor(failure: GenerateDslFailure, message: string, reason?: string);
}
```

Thrown by `generateDsl`. See [rule builders](https://docs.ana.fi/sdk/rule-builders.md#generate-a-document-from-text).

## HTTP error mapping

| Route | Method | No answer or timeout | Status not 2xx | Bad body |
| - | - | - | - | - |
| `POST /api/wallets/:aa/dsl/commit/stage` | `stageDsl`, `stageErc20AllowDsl` | `BackendRequestError`, `status: null` | `409` with a sealed `stale_parent` reason: `DslStaleParentError`. A sealed `notification_email_not_verified` reason: `NotificationEmailNotVerifiedError`. Any other sealed reason: `DslStageRefusedError`. No sealed reason, or one that does not open: `BackendRequestError`. | `Error` |
| `POST /api/wallets/:aa/recover` | `recover` | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `POST /api/wallets/:aa/notification_email/request_code`, `.../verify_code` | the notification methods | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `POST /api/wallets/provision` | `provisionPredictedSafe`, `stageDsl` | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `POST /api/wallets/:aa/safe/sponsor_exec` | the Safe owner operations | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `POST /api/pay/receipt_email` | `sendPayReceiptEmail` | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `GET /api/fees` | `fetchFeeTerms` | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `GET /api/wallets/:aa/sponsorship` | `fetchWalletSponsorship` and every Safe owner operation | `BackendRequestError`, `status: null` | `BackendRequestError` | `Error` |
| `GET /api/wallets/:aa/activity` | `getActivity` | the `fetch` error | `Error("getActivity(<aa>) failed: <status> <body>")` | `Error` for an unknown `kind` |
| `GET /api/accounts/discover` | `discoverAccounts` | the `fetch` error | `Error("GET <url> failed: <status> <body>")` | rows dropped, completeness `unrecognized_completeness` |
| `POST /api/dsl/generate` | `generateDsl` | the `fetch` error | `GenerateDslError`: `404` `unavailable`, `401` `signIn`, other `failed` | `GenerateDslError` `failed` when `yaml` is absent or empty. A body that is not JSON rejects with the JSON parse error. |
| the status endpoint | `readEngineSyncedHeight` | `null` | `null` | `null` |

Timeouts: 180 seconds for each `POST` above except `generateDsl` (45 seconds); 10 seconds for fees, sponsorship and activity; 15 seconds for discovery.

Chain reads and wallet requests do not go through this table. A failed chain read rejects with the viem error (see [reads](https://docs.ana.fi/sdk/reads.md#the-rule-for-a-failed-read)). A wallet rejection rejects with the wallet error.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.