> ## 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

> The shape of each error body, the meaning of each status code, and the SDK error class that each one becomes.

After you read this page, you can tell from a status code and a body what went wrong and what to do next. You also know which error class the SDK throws for it.

## Rules for all routes

* Branch on the HTTP status code. Branch on a JSON code only where this page names one.
* A plain-text body is a short English sentence. Show it or log it. Do not parse it, because the words can change.
* A route answers `200` also for some results that are not a success. Read the result field: `outcome.kind` of the receipt route, and `outcome.kind` inside the sealed reply of the email routes.

## Body shape per route

| Route | Error body |
| - | - |
| `GET /api/accounts/discover` | Plain text |
| `POST /api/wallets/provision` | Plain text. `403` can also be JSON `{"code":"account_not_sponsored"}`. |
| `POST /api/wallets/{aa}/safe/sponsor_exec` | Plain text. `403` can also be JSON `{"code":"account_not_sponsored"}`. |
| `POST /api/wallets/{aa}/dsl/commit/stage` | Plain text, or JSON `{"error":"invalid_dsl","sealed_error":...}` for `409` and some `422`. |
| `POST /api/wallets/{aa}/recover` | Plain text |
| `POST /api/wallets/{aa}/notification_email/request_code` | Plain text |
| `POST /api/wallets/{aa}/notification_email/verify_code` | Plain text |
| `GET /api/wallets/{aa}/activity` | Plain text |
| `GET /api/wallets/{aa}/sponsorship` | Plain text |
| `GET /api/fees` | Plain text |
| `POST /api/pay/receipt_email` | Plain text |
| `POST /api/dsl/generate` | JSON `{"error":"..."}`, a sentence for the user. A malformed body gives plain text. |
| Bank payout routes | JSON `{"error":"..."}`. A malformed body or path gives plain text. |

## Errors before the route runs

These errors come before the handler of a route reads the request. They have a plain-text body.

| Status | Cause | Example body |
| - | - | - |
| `400` | The `aa` path segment is not an address. | `Invalid URL: invalid string length` |
| `400` | A required query parameter is missing or not valid. | ``Failed to deserialize query string: missing field `eoa` `` |
| `400` | The body is not valid JSON. | `Failed to parse the request body as JSON: EOF while parsing an object at line 1 column 1` |
| `413` | The body is larger than the limit of the route. | |
| `415` | The `Content-Type: application/json` header is missing. | ``Expected request with `Content-Type: application/json` `` |
| `422` | The JSON is valid, but a field is missing or has the wrong type or format. | ``Failed to deserialize the JSON body into the target type: missing field `eoa` at line 1 column 2`` |
| `404` | The path does not exist on this host, for example a bank payout route on `polygon`. | empty |
| `405` | The path exists, but not with this method. | empty |

A `400` or a `422` with such a body is a defect in your request. Fix the request. Do not retry it unchanged.

## Errors from the engine

These routes pass a sealed request to the engine: `stage`, `recover`, `notification_email/request_code` and `notification_email/verify_code`. When the engine refuses, the Anafi API returns the status and the body of the engine without change. When the engine cannot be reached, the Anafi API answers `502` with the body `engine unreachable`.

The bodies of the engine do not contain your plaintext. The reason for a refused rule document is sealed to you. See [Sealed requests](https://docs.ana.fi/api-reference/sealed-requests.md#open-a-staging-refusal).

## Status codes

| Status | Meaning in the Anafi API | What to do |
| - | - | - |
| `200` | The request succeeded. Some routes put a result that is not a success in the body. | Read the body. |
| `400` | The request has a defect: path, query, JSON syntax, or a value that the route refuses before any work. On the Safe relay, also a network or account mismatch, or calldata that is not a Safe transaction of a known form. On `POST /api/dsl/generate`, an input limit. | Fix the request. |
| `401` | Only `POST /api/dsl/generate`: the sign-in does not verify or its session ended. | Sign a new session message. |
| `403` | Anafi does not do or pay for this. `{"code":"account_not_sponsored"}` means that fees are on and the account has no sponsor. A plain-text `403` on the Safe relay names the call or the check that is not allowed. On bank payout routes, the Safe or the signer does not prove ownership. | For `account_not_sponsored`, send the transaction from the owner address. For the others, change the transaction or the signer. |
| `404` | The route is not served on this host, or (bank details only) no Monerium account is connected. | Use a host that serves the route, or connect first. |
| `409` | Only staging: the on-chain document changed since you built yours (`stale_parent`). | Read the current document, rebuild on it, stage again. |
| `413` | The body is too large. | Send a smaller body. |
| `415` | The JSON content type is missing. | Add `Content-Type: application/json`. |
| `422` | The request is well formed, but the engine or the route refuses its content: a rule document that is not valid, an envelope that does not open, a sign-in that fails a check, a used sign-in nonce, an adapter that is not live, an amount that is not valid, or a generation that produced no document. | Show the reason to the user. For a sign-in failure, sign a new message. |
| `429` | A limit is reached: too many staged documents (`staged_dsl_cap_reached`), another relay for the same Safe in progress, or a daily generation limit. | Wait, then try again. |
| `500` | The Anafi API or the engine failed to store, sign or seal. | Try again later. |
| `502` | A dependency failed: the engine could not be reached, a chain read failed, or a submission failed. | Try again later. On the Safe relay, read the Safe nonce first. |
| `503` | The Anafi API could not get an answer it needs before it acts: the fee terms, the sponsor record, or (bank payouts) a chain read. Also a network that serves no Safe accounts. | Try again later. For a deploy or a Safe transaction, the owner can send it instead. |
| `504` | Only the Safe relay: the transaction was sent, but not mined in time. It can still be mined. | Do not send the same bytes again. Read the Safe nonce. |

The [API reference](https://docs.ana.fi/api-reference/introduction.md#all-operations) lists the exact statuses of each operation.

## SDK error classes

The SDK turns a failed HTTP call into the classes below. See [SDK errors](https://docs.ana.fi/sdk/errors.md) for the full list.

| Route | Failure | SDK error |
| - | - | - |
| `provision`, `sponsor_exec`, `stage`, `recover`, `notification_email/*`, `pay/receipt_email` | No answer (network failure or time limit) | `BackendRequestError` with `status: null`. The cause is in `cause`. |
| Same routes | Any status that is not `2xx` | `BackendRequestError` with `status` and `responseText` |
| `stage` | `422` with an `invalid_dsl` body that opens | `DslStageRefusedError`, with the reason of the engine in `refusal` |
| `stage` | `422` with `reason: "notification_email_not_verified"` | `NotificationEmailNotVerifiedError`, a subclass of `DslStageRefusedError` |
| `stage` | `409` with `reason: "stale_parent"` | `DslStaleParentError`, with the current on-chain document in `current` |
| `GET /api/fees`, `GET /api/wallets/{aa}/sponsorship` | No answer, or a status that is not `2xx` | `BackendRequestError` |
| `GET /api/wallets/{aa}/activity`, `GET /api/accounts/discover` | A status that is not `2xx` | `Error`, with the status and the body in the message |
| `POST /api/dsl/generate` | `404` | `GenerateDslError` with `failure: "unavailable"` |
| `POST /api/dsl/generate` | `401` | `GenerateDslError` with `failure: "signIn"` |
| `POST /api/dsl/generate` | Any other status that is not `2xx` | `GenerateDslError` with `failure: "failed"`, and the sentence of the route in `reason` |

`DslStaleParentError` is not a subclass of `BackendRequestError`. Check for it first.

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

try {
  await client.stageDsl(params);
} catch (error) {
  if (error instanceof DslStaleParentError) {
    // Rebuild the document on error.current, then stage again.
  } else if (error instanceof DslStageRefusedError) {
    // Show error.refusal to the author.
  } else if (error instanceof BackendRequestError && error.status === null) {
    // No answer. Try again later.
  } else {
    throw error;
  }
}
```


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