# Errors

Every `/api/v1/*` response uses the same JSON error envelope. Failures are typed, the codes are stable, and the wire shape never changes within `v1`.

## Wire shape

```json
{
  "error": "human-readable message",
  "code": "VALIDATION_ERROR",
  "details": {
    "subcode": "ACCEPTANCE_REQUIRED"
  },
  "requestId": "5dba8a3c-9b71-4f9b-9b9a-3b8b2a1c4d2e"
}
```

- `error` - short message intended for humans. Don't pattern-match on it; it can change between versions.
- `code` - stable enum value. Pattern-match on this.
- `details` - present on some codes, and what it holds depends on the code. The `subcode` field, when present, is a stable discriminator within the parent code (e.g. an `API_V1_IDEMPOTENCY_BODY_MISMATCH` subcode under a `CONFLICT` parent). A body that fails validation carries `details.issues` instead: see [Validation errors in detail](#validation-errors-in-detail).
- `requestId` - UUID for support correlation. Quote this when filing a ticket.

## Codes and HTTP status mapping

| Code | HTTP | When it fires |
| --- | --- | --- |
| `VALIDATION_ERROR` | 400 | The request body, query parameters or headers failed validation. For a body, `details.issues` lists each field and what is wrong with it. Subcode `ASSIGNEE_NOT_ON_REGISTER`: a risk's owner, a review's assignee or a treatment action's owner can't read the register (only an org owner or admin, or someone on the register, can be given its work). Subcode `ACCEPTANCE_REQUIRED`: a create, edit, bulk change or treatment tried to make a risk `accepted`, which only `POST /api/v1/risks/{id}/accept` does (an accepted risk sent back with its status unchanged is fine). |
| `UNAUTHORIZED` | 401 | A missing, malformed, unknown or revoked Bearer token, or one whose secret doesn't match. All five answer with this code and the same message, so a caller cannot use the response to find out which tokens exist ([Authentication](https://riskregister.com/docs/authentication.md#failure-modes) lists them). One more, for the token's holder only: a personal access token past its expiry is refused with the date it expired (the check runs after the token matches). |
| `FORBIDDEN` | 403 | Authenticated, but the caller cannot perform this action. The subcodes are listed under [Forbidden subcodes](#forbidden-subcodes). Two refusals carry a message only, with no subcode: a personal access token or connected app while Risk Register has switched tokens or programmatic access off for the organisation, and a non-admin's token on a route reserved for admins (see [Authentication](https://riskregister.com/docs/authentication.md#failure-modes)). |
| `NOT_FOUND` | 404 | The entity doesn't exist, isn't visible to your organisation, or has been deleted. A record in another organisation answers exactly as one that doesn't exist, so the response never reveals whether it does. |
| `METHOD_NOT_ALLOWED` | 405 | The HTTP method isn't supported for this resource. The response carries an `Allow` header listing the methods that are. Used today on `DELETE /api/v1/registers/{id}` (archive the register with `POST /api/v1/registers/{id}/archive` instead). |
| `CONFLICT` | 409 | State machine violation. Subcodes include `API_V1_IDEMPOTENCY_BODY_MISMATCH` (same `Idempotency-Key`, different body), `MAPPING_DIFFERS_FROM_REQUEST` (re-PUT mapping with a different body), `REVIEW_NOT_PENDING` (cannot complete or cancel a non-pending review), `AI_GENERATION_IN_FLIGHT` (carries `Retry-After`), `API_V1_DRAFT_DISCARDED` (a retried document save or template start whose draft a person has since discarded in the app; a new save needs a new `Idempotency-Key`), `API_V1_REGISTER_NOT_ACTIVE` (the register is archived or closed, so it is read-only: no change to its risks, their reviews, treatment actions, control links, acceptances or comments until someone makes it active again; `details.registerId` and `details.status`), `API_V1_RISK_CLOSED` (a closed risk takes no new treatment action, review request or acceptance; reopen it first; `details.riskId`). |
| `PRECONDITION_FAILED` | 412 | The `x-rr-if-match` header didn't match the current ETag. Subcodes: `ETAG_MISMATCH` (stale ETag), `IF_MATCH_MALFORMED`, `IF_MATCH_WILDCARD_UNSUPPORTED`. The `ETAG_MISMATCH` body carries `details.currentETag` and `details.suppliedETag`. See [ETag and optimistic concurrency](https://riskregister.com/docs/etag-concurrency.md). |
| `PAYLOAD_TOO_LARGE` | 413 | The request body exceeded the cap: 64 KiB on most routes, 1 MiB on a register document save (`POST /api/v1/registers/from-document`) and a proposal. |
| `UNPROCESSABLE` | 422 | The request is well formed but can't be applied as asked. Used today for a proposal that can't be stored as it stands: subcode `API_V1_PROPOSAL_REJECTED`, with the refused rows and the reason for each in `details.rejected`. See [Drafts and proposals](https://riskregister.com/docs/drafts-and-proposals.md). |
| `PRECONDITION_REQUIRED` | 428 | A proposal was sent without `expectedRevision`, the register revision it was written against. |
| `RATE_LIMITED` | 429 | Per-token or per-IP rate limit exhausted. Response carries `Retry-After: <seconds>` header. See [Rate limits](https://riskregister.com/docs/rate-limits.md). |
| `INTERNAL_ERROR` | 500 | Unexpected failure. The `requestId` is the only useful signal for the customer; the server logs the full trace under that id. |
| `EXTERNAL_SERVICE_ERROR` | 503 | A service Risk Register depends on failed: for example Anthropic (AI generation), Clerk (sign-in) or Stripe (billing). Transient; retry with exponential backoff. |
| `SERVICE_UNAVAILABLE` | 503 | Paused on purpose by Risk Register, not a failure; nothing was done. Only the MCP endpoint answers with it today: subcode `MCP_PAUSED` (the whole endpoint) or `MCP_WRITES_PAUSED` (a tool that would change something, while reading still works). See [the MCP guide](https://riskregister.com/docs/mcp.md#when-the-endpoint-is-paused). |

The table lists every code. The same list is the `Error.code` enum in the [OpenAPI specification](https://riskregister.com/docs/api/openapi.yaml).


### Forbidden subcodes

Who is asking:

- `ORG_SUSPENDED`: Risk Register has suspended the organisation.
- `API_KEY_CREATOR_PERMISSION_REVOKED`, `PAT_OWNER_PERMISSION_REVOKED`: the person the token acts as no longer has the permission the write needs.
- `INSUFFICIENT_SCOPE`: the token's access level doesn't allow the request. `details.required` and `details.granted` say which levels.

What the plan allows:

- `PLAN_LIMIT`: a plan limit was reached, and upgrading lifts it. `details.limit` names the limit and `details.cap` is its number.
- `OPERATOR_LIMIT`: the same, but the limit was set by Risk Register for this organisation, so upgrading won't lift it.
- `FEATURE_NOT_ENABLED`: the plan lacks the feature named in `details.feature`.
- `FEATURE_TURNED_OFF`: the plan has the feature, and Risk Register has turned it off for this organisation.
- `TRIAL_LIMIT`: the AI allowance of a Team trial is used up. `details.limit` is `ai_generations_per_trial` or `ai_calls_per_trial`, and `details.cap` is its number.

The values of `details.limit`:

| `details.limit` | What is counted |
| --- | --- |
| `registers` | Registers |
| `users` | People in the organisation |
| `live_risks` | Risks that aren't closed, across every register (300 on Free) |
| `templates` | Registers started from a template |
| `ai_calls_per_month` | AI calls in the calendar month |
| `ai_provider_attempts_per_month` | Attempts made to the AI provider in the calendar month |

A refused batch is refused whole.

## Validation errors in detail

When a request body fails validation, `details.issues` lists every problem found. Each has a `path` (the field, with nested fields joined by dots, such as `risks.0.title`) and a `message` written for a person:

```json
{
  "error": "request body is invalid",
  "code": "VALIDATION_ERROR",
  "details": {
    "issues": [
      {
        "path": "title",
        "message": "Too small: expected string to have >=1 characters"
      },
      {
        "path": "categoryId",
        "message": "Invalid UUID"
      }
    ]
  },
  "requestId": "..."
}
```

Don't parse `message`: its wording can change. Other validation failures (a malformed query parameter, a rule that spans fields) carry a message and sometimes a `subcode`, without `issues`.

Score-field bounds depend on the parent register's scoring matrix (1–5 on `iso31000_5x5`, 1–3 on `simple_3x3`). The server checks a score against the register's own matrix; the OpenAPI schema documents both ranges.

## Retry semantics

| Code | Safe to retry? | How |
| --- | --- | --- |
| `RATE_LIMITED` | Yes | Wait the number of seconds in `Retry-After`, then retry. Many HTTP client libraries can do this for you. |
| `CONFLICT` with subcode `AI_GENERATION_IN_FLIGHT` | Yes | Carries `Retry-After`. Same-key replay returns the original result once the in-flight call completes. |
| `EXTERNAL_SERVICE_ERROR` | Yes | Exponential backoff. The upstream is transient. |
| `SERVICE_UNAVAILABLE` | Later | A pause lasts until Risk Register lifts it: minutes or hours, not seconds. Don't retry in a loop; tell the person, and try again later. |
| `INTERNAL_ERROR` | Yes, once | Quote `requestId` if it recurs. |
| `PRECONDITION_FAILED` | No, not without re-reading | Re-GET the resource to capture the fresh ETag, then retry with the new value in `x-rr-if-match`. |
| `CONFLICT` other subcodes | No | Action-specific; the body tells you why. |
| `VALIDATION_ERROR`, `NOT_FOUND`, `FORBIDDEN`, `UNAUTHORIZED`, `METHOD_NOT_ALLOWED`, `PAYLOAD_TOO_LARGE`, `UNPROCESSABLE`, `PRECONDITION_REQUIRED` | No | Fix the request; retrying with the same body returns the same error. |

## Idempotency-safe retries

For state-mutating routes, supply an `Idempotency-Key` header to make every retry safe regardless of error code. Same key plus same body returns the record the first call made, without making another; same key plus a different body returns 409 `API_V1_IDEMPOTENCY_BODY_MISMATCH`. See [Idempotency](https://riskregister.com/docs/idempotency.md).

## Dispatching on errors

Most HTTP clients can dispatch on status code; for finer control, dispatch on `code` from the parsed body. It is stable within `v1`, and the table above lists every value.

## See also

- [Authentication](https://riskregister.com/docs/authentication.md) - token shapes, 401 vs 403 semantics.
- [Rate limits](https://riskregister.com/docs/rate-limits.md) - 429 + `Retry-After` contract.
- [ETag and optimistic concurrency](https://riskregister.com/docs/etag-concurrency.md) - 412 deep-dive.
- [Idempotency](https://riskregister.com/docs/idempotency.md) - making retries safe.
