# Errors

> The cairnflow error body, HTTP status codes, machine-readable error codes, invocation failure codes, and what clients should retry.

Source: https://cairnflow.dev/docs/reference/errors

Two kinds of failure come back from the API, and they are easy to tell apart:

- **The request failed.** The body is the common error body below.
- **The invocation reached an outcome.** `/invoke`, `/send`, workflow and attach routes answer with
  a body that has a `status` field (`failed`, `paused`, `throttled`, ...). See
  [Invoking handlers](/docs/reference/http-api#invoking-handlers).

## The error body

```json
{ "error": "a sentence for humans", "code": "range_overloaded", "retry_after_ms": 1000 }
```

| Field | Present | Meaning |
|---|---|---|
| `error` | always | A message for people. Do not parse it. |
| `code` | some errors | A stable machine-readable code (see below). |
| `leader` | some `503` | The client address of the node that leads the range. Retry once there. |
| `id` | when known | The invocation the error is about. Its existence is not guaranteed: look it up, or resend with the same `Idempotency-Key`. |
| `retry_after_ms` | some `429` | When to try again. A `Retry-After` header (seconds) accompanies it. |
| `table` | `409 wrong_range` | The current range table, for internal callers. |

Unexpected internal errors answer `500` with a generic message that includes a reference code; the
details are only in the node's log.

## HTTP status codes

| Status | Meaning | What to do |
|---|---|---|
| `400` | Bad request: malformed parameter, invalid header, limit key without scope, forbidden SQL. | Fix the request. |
| `401` | Missing or invalid key. | Check the key. |
| `403` | The key's role does not allow this route, or the tenant is suspended or being deleted. | Use a key with the right role. |
| `404` | Not found, including an invocation of another tenant. | Do not retry. |
| `409` | Conflict: invocation `paused` or `already_invoked` (as a `status`), purge of a running invocation, resume of an invocation that is not paused, deployment still in use or being removed, revision moved under you, disabled feature gate. | Read the state, then decide. |
| `410` | Stream expired. | Do not retry. |
| `413` | Request body over 2 MiB, payload larger than the tenant's limit (`payload_too_large`), or an output too large to return in one response (`value_too_large`). | Send less; use artifacts for large data. |
| `422` | SQL query over budget (the `error` starts with `query_budget:`). | Narrow the query, add `LIMIT`. |
| `429` | Too many requests or over a limit. See the codes below: some mean the work was accepted, some mean nothing was created. | Wait for `Retry-After`. |
| `500` | Internal error, or `status: failed` on invoke routes. | For a generic `500`, retry with the same `Idempotency-Key`. |
| `502` | The node lost the operation before it could answer (for example while accepting an invocation, or stepping down as leader). | If an `id` is present, look it up; otherwise resend with the same `Idempotency-Key`. |
| `503` | Not the leader (with `leader`), the cluster is electing a leader or changing its range table (without `leader`), or storage is briefly unavailable (`store_unavailable`). | With `leader`: retry once there. Without: back off and retry. |
| `504` | A forwarded request (`x-cairn-forward: leader`) was sent but no response came back. The operation may have run. | Check state, or resend with the same `Idempotency-Key`. |

## Error codes

| `code` | Status | Meaning |
|---|---|---|
| `range_overloaded` | `429` | The range is overloaded (compaction is behind). **Nothing was created**, no `id`. Resending after `Retry-After` is safe. |
| `tenant_limited` | `429` | The tenant is in the `limited` state. Nothing was created. |
| `live_invocations_limit` | `429` | The tenant has reached its limit of live invocations. Nothing was created. |
| `deploy_rate_limited` | `429` | Too many deployment registrations for the tenant. Retry after `Retry-After`. |
| `sse_limit` | `429` | The node has reached its limit of open event streams. Reconnect after `Retry-After`. |
| `too_many_open_streams` | `429` | The tenant has reached its limit of long-lived connections on this node. |
| `value_assembly_busy` | `429` | The tenant already has 4 large outputs being assembled on this node. Retry after `Retry-After`. |
| `tenant_suspended` | `403` | The tenant is suspended. |
| `tenant_deleting` | `403` | The tenant is being deleted or has been deleted. |
| `egress_denied` | `400` | The node's outbound policy refused the deployment's endpoint. The message says which address and why. |
| `identity_required` | `400` | The node requires services that verify signed requests. Set `identityKeys` in `serve()` and register again. |
| `compression_required` | `400` | The node requires request compression for request-response deployments. Enable `compression` (the TypeScript SDK default) and register again. |
| `payload_too_large` | `413` | The request body exceeds the tenant's payload limit. |
| `value_too_large` | `413` | An output larger than 64 MiB cannot be returned in one response. |
| `deployments_limit` | `409` | The tenant has reached its limit of deployments. Remove an old one. |
| `schedules_limit` | `409` | The tenant has reached its limit of schedules. |
| `schedules_disabled` | `409` | Server-side schedules are not enabled on this cluster yet. |
| `key_roles_disabled` | `409` | `view` and `provision` keys are not enabled on this cluster yet. |
| `wrong_range` | `409` | Internal: a node addressed a range with a stale table. |
| `stream_expired` | `410` | The stream is no longer kept. |
| `store_unavailable` | `503` | The cell's storage failed for a moment. Nothing about the storage is in the body. Retry later. |
| `keys_busy` | `503` | Too many key changes at once. Nothing was written; retry after `Retry-After`. |

SQL query limits have no `code` field: the `error` of a `422` (over the read, memory, time or row
budget) or of a `429` (the tenant used up its read units) starts with `query_budget:`.

Codes for operators of multi-tenant nodes also exist (`tenant_state_disabled`,
`tenant_purge_disabled`, `tenant_purging`, `tenant_purged`), returned by the tenant administration
routes.

### `429 throttled` versus `429 range_overloaded`

They look alike and mean opposite things:

- **`status: "throttled"`** in an invoke body: the invocation **was accepted**, is durable, and will
  run when the limit allows. The body has an `id`. Do not resend; wait or attach.
- **`code: "range_overloaded"`**: **nothing was created**. Resend after `Retry-After`. With the same
  `Idempotency-Key` (or the same workflow id) the resend maps to a single invocation. An invocation
  that already exists never gets this answer.

## Invocation failure codes

A `failed` or `paused` invocation carries a numeric `code` and a `message`. Other codes come from your
handler (the code of a `TerminalError`, HTTP-like). The runtime uses:

| Code | Meaning |
|---|---|
| `570` | Journal mismatch: the handler took a different path on replay than the journal records (for example, a renamed step). |
| `571` | Protocol violation by the service, including a state write (`set`, `clear`, `clearAll`) from a `SHARED` handler. |
| `572` | Cancelled. |
| `573` | Workflow already invoked. |
| `574` | Missing object key for a virtual object handler. |
| `575` | Retry attempts exhausted (`retryPolicyMaxAttempts`). The message includes the last error. |
| `576` | Quarantined: new invocations of a handler that keeps failing are held. |
| `577` | Stuck on infrastructure: the runtime cannot move the invocation forward. |
| `578` | Journal longer than the tenant's limit. |
| `579` | Over a tenant resource limit. The message starts with `timers_limit`, `state_keys_limit` or `artifacts_limit`. |
| `413` | An output or state value larger than the tenant's payload limit. |

A `paused` invocation keeps its place; fix the cause (deploy a fix, or migrate it), then
`POST /invocations/{id}/resume`. Resuming a `570` without changing the deployment reproduces the
same mismatch.

## What to retry

- **Always resend with an `Idempotency-Key`.** Without one, a retry after a timeout or a `502` can
  run the side effect twice. With one, the retry points to the same invocation and returns its
  result or its current state. Keys are remembered for the retention period (24 hours by default).
- **`503` with `leader`**: once, to that address. The CLI and the TypeScript SDK client do this for
  you.
- **`503` without `leader`**, **`429` with `Retry-After`**, generic **`500`**: back off and retry.
- **`202 waiting`**: not an error. Attach (`POST /invocations/{id}/attach?wait_ms=25000`) or poll
  `GET /invocations/{id}`.
- **`4xx` other than `429`**: do not retry unchanged.
- **Workflows**: a workflow's `run` must not carry an `Idempotency-Key`; the workflow id already
  deduplicates. A second call gets `409 already_invoked` with the id; attach with
  `/workflow/{service}/{key}/attach`.
