# Errors

> The error format, what each status means, and when and how to retry.

## Error format

Failed requests return a JSON:API document with an `errors` array. Branch on the HTTP status and on `code`; never on `title` or `detail`, which are for people and can change.

```json
{
  "errors": [
    {
      "status": "422",
      "code": "validation.invalid",
      "title": "Invalid Attribute",
      "detail": "The family name is required.",
      "source": { "pointer": "/data/attributes/familyName" }
    }
  ]
}
```

| Member             | Meaning                                                                    |
| ------------------ | -------------------------------------------------------------------------- |
| `status`           | The HTTP status, as a string                                               |
| `code`             | A stable, machine-readable code such as `verification.failed`              |
| `title`            | A short summary of the code                                                |
| `detail`           | What went wrong in this request                                            |
| `source.pointer`   | The member of your request body that caused it                             |
| `source.parameter` | The query parameter that caused it                                         |
| `meta`             | Extra facts for some codes, such as `meta.reason` on a failed verification |

## Status codes

| Status       | Meaning                                                                                    | What to do                                                                                  |
| ------------ | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------- |
| `400`        | The query string is invalid: an unknown filter, sort or include                            | Fix the request                                                                             |
| `401`        | The key or token is missing, invalid, expired or revoked                                   | Check the credential; for capture and document tokens, their expiry and origin              |
| `403`        | The credential lacks a scope, or something your account turned off (`capability.disabled`) | Add the scope named in `detail`, or ask an owner to turn it on in the organization settings |
| `403`        | Your organization is suspended (`organization.suspended`), for every key and token           | Contact Aqoonsi support; nothing in the request will change it                              |
| `404`        | The resource does not exist, or belongs to another account or environment                  | Check the ID and the key's environment                                                      |
| `406`, `415` | Wrong `Accept` or `Content-Type`                                                           | Send `application/vnd.api+json`                                                             |
| `409`        | The request conflicts with the resource's state, such as changing a resolved case          | Read the resource and decide                                                                |
| `412`        | `If-Match` is stale                                                                        | Fetch the resource again and reapply your change                                            |
| `422`        | The body is invalid, or a verification failed                                              | Read `source.pointer` or `meta.reason`                                                      |
| `428`        | `If-Match` is required                                                                     | Send the resource's `ETag`                                                                  |
| `429`        | Rate limited                                                                               | Retry after `Retry-After`                                                                   |
| `503`        | Aqoonsi or a source behind it is unavailable                                               | Retry after `Retry-After`                                                                   |

## Outages are not results

When a registry, face or document source is down or busy, the API returns `503` or `429`, records a `providerError` check and leaves the verification open. An outage is never reported as a failed verification, a screening that found nothing, or a business that does not exist.

Never reject a customer because of a `503` or `429`. Retry later, or let the person try again.

## Retrying

Retry `429`, `503` and `409 idempotency.inFlight`. Do not retry other `4xx` responses unchanged.

- Wait for `Retry-After` when it is present; otherwise back off exponentially with jitter.
- Retry a create with the **same** `Idempotency-Key` and body, so it never runs twice. See [idempotency keys](requests.md#idempotency-keys).
- After a failed verification step, read the verification before deciding what to do next.

```js
async function withRetry(request, attempts = 5) {
  for (let attempt = 1; ; attempt++) {
    const response = await request();
    if (![429, 503].includes(response.status) || attempt === attempts)
      return response;
    const retryAfter = Number(response.headers.get("Retry-After"));
    const delay =
      retryAfter > 0
        ? retryAfter * 1000
        : 2 ** attempt * 250 * (1 + Math.random());
    await new Promise((resolve) => setTimeout(resolve, delay));
  }
}
```

## Support

Every response has an `X-Request-ID` header. Quote it when you contact support. Leave personal data, credentials and verification payloads out of support requests and of your logs.
