# Errors

Source: https://docs.formance.com/reference/api/errors

Stack: v3.2

Errors are JSON with a stable machine-readable `errorCode` and a human `errorMessage`.
Branch on `errorCode` — not on the message text, and not on the HTTP status alone.

## Error shape

Every module returns the same envelope. `errorCode` is the contract: it is stable across
versions and safe to switch on. `errorMessage` is for your logs and may be reworded at any
time. `details` is optional and rarely populated — treat it as opaque free text for humans,
never as a parseable structure. Note that the OpenAPI contract still carries a legacy example
showing a `play.numscript.org` link; nothing in the current Ledger emits a URL there, so do not
build a link out of it.

| Field          | Type         | Notes                                                                                                                                 |
| -------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
| `errorCode`    | string, enum | Stable machine-readable code. The value you branch on.                                                                                |
| `errorMessage` | string       | Human-readable explanation. Not stable — do not parse it.                                                                             |
| `details`      | string       | Optional, usually absent. Where set, it is the underlying error text — not a URL and not structured. Omitted on `COMPILATION_FAILED`. |

```json
{
  "errorCode": "INSUFFICIENT_FUND",
  "errorMessage": "account had insufficient funds: user:john:wallet, USD/2"
}
```

## Common error codes

`INSUFFICIENT_FUND` and `CONFLICT` are the two worth explicit handling — the first blocks
a transaction from posting, the second signals the operation already landed.

| Code                 | Status | What it means                                                                                                                        |
| -------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| `VALIDATION`         | 400    | The request is malformed — unknown filter field, both `postings` and `script`, bad date. Fix the call.                               |
| `INSUFFICIENT_FUND`  | 400    | A source account cannot cover a posting. Either fund it, or pass `force` if a negative balance is legitimate for that account.       |
| `COMPILATION_FAILED` | 400    | Numscript did not compile. The compiler diagnostics are in `errorMessage`; `details` is not set on this error.                       |
| `CONFLICT`           | 409    | The `reference` is already taken in this ledger. Usually means the transaction already succeeded — fetch it rather than retrying.    |
| `NOT_FOUND`          | 404    | The ledger, transaction or account does not exist. Note that reading a never-used account address returns a zero balance, not a 404. |

## What to retry

Retry `429` and `5xx` with exponential backoff. A `4xx` is otherwise deterministic — the
request will fail identically, so fix it rather than repeating it.

`401` is the one exception. Tokens expire (one hour by default), and a missing or expired
token is reported the same way as an invalid one — `401` with no body detail — so the status
alone does not tell the two apart. Refresh the token once and replay the request; if the
replay is still `401`, the credentials themselves are wrong and further attempts will not
help. Cap it at a single refresh-and-retry per request so bad credentials cannot become a
refresh loop.

The SDKs handle the token cache but not the replay: on a `401` the client-credentials hook
discards the cached session so the _next_ call fetches a fresh token, and it acquires a token
before a request when none is cached. The failed request itself is still returned to you, so
the single retry above is yours to make.

`Idempotency-Key` is what makes retrying an ambiguous write safe, but it is narrower than
"Ledger and Wallets". It is honored on the Ledger v2 **data-plane** writes (create and revert
transaction, transaction and account metadata write/delete, create schema) and on the Wallets
write endpoints, which forward it to the Ledger. Where it is honored, send the same key on
every retry: if the original call landed, the Ledger replays that response, so retrying a
timed-out POST is safe.

It is **not** honored on Ledger v2 admin and control operations (ledger creation, `_bulk`,
ledger-level metadata, buckets, exporters, pipelines, log import/export), on any Ledger v1
endpoint, or on Payments, Reconciliation and Auth. Retrying one of those after a timeout can
duplicate the effect, so read current state first and retry only if the write did not land.

Webhooks is a special case that cuts the other way: its delivery replay endpoints **require**
`Idempotency-Key` rather than merely accepting it, and cache the replay result for 24 hours,
so a replay retried with the same key returns the original outcome instead of replaying twice.
Reusing that key for a different replay is a conflict. No other Webhooks endpoint takes the
header. See [Conventions](https://docs.formance.com/reference/api/conventions) for the per-endpoint breakdown, and
treat the endpoint's own entry in the API reference as authoritative.

| Condition                                                     | Retry | How                                                                                        |
| ------------------------------------------------------------- | ----- | ------------------------------------------------------------------------------------------ |
| `401`                                                         | once  | Refresh the token, then replay once. A second `401` means invalid credentials, not expiry. |
| `429`                                                         | yes   | Honor `Retry-After`; back off exponentially with jitter.                                   |
| `500`, `502`, `503`, `504`, endpoint honors `Idempotency-Key` | yes   | Retry with backoff and the same key.                                                       |
| `500`, `502`, `503`, `504`, endpoint does not                 | yes   | Retry with backoff. Read first if the operation may have landed.                           |
| timeout / connection reset, endpoint honors `Idempotency-Key` | yes   | Retry with the same key — that is precisely what it is for.                                |
| timeout / connection reset, endpoint does not                 | maybe | Check current state first; retry only if the write did not land.                           |
| `403` or any other `4xx`                                      | no    | Deterministic. Fix the request — a `403` needs a token with the right scope, not a retry.  |
