# Errors

> Every error has the same JSON shape, a machine code and a request id.

Source: https://developers.loyx.se/guides/errors/

Errors use real HTTP status codes and one JSON shape:

```json
{
  "error": {
    "type": "not_found_error",
    "code": "membership_not_found",
    "message": "No active membership matches this identifier.",
    "param": "value",
    "doc_url": "https://developers.loyx.se/guides/errors/"
  },
  "request_id": "3f2c9a1e-0000-4000-8000-000000000004"
}
```

| Field | Use it for |
|---|---|
| `error.type` | The broad class. Branch on it when you do not know the `code`. |
| `error.code` | The exact reason. Stable: we never change the meaning of a code. Branch on this. |
| `error.message` | Text for your logs and developers. Not for the cashier, and it may change. |
| `error.param` | The field that caused it, when there is one. |
| `error.doc_url` | A link to this page. |
| `error.details` | On `422 invalid_fields` only: one entry per failing field. |
| `request_id` | Quote it when you contact support. It is also in the `X-Request-ID` response header. |

You can send your own `X-Request-ID` (letters, digits, `.`, `_`, `-`, up to 64 characters). We echo it back and use it in our logs, so one id follows the call through both systems.

## Types

| HTTP | `error.type` | Meaning |
|---|---|---|
| 400 | `invalid_request_error` | The request is wrong in a way the schema cannot express. |
| 401 | `authentication_error` | No token, or the token is invalid or expired. Get a new token and retry once. |
| 403 | `authorization_error` | The token is valid but not allowed to do this, for example `insufficient_scope`. |
| 404 | `not_found_error` | The resource does not exist for this brand. |
| 409 | `conflict_error` | The request clashes with existing data, for example a duplicate id. |
| 422 | `validation_error` | A field failed validation. See `error.details`. |
| 429 | `rate_limit_error` | Too many requests. Wait for `Retry-After` seconds. |
| 500 | `internal_error` | Our fault (`internal_server_error`, `database_error`). Retry with backoff; quote the `request_id` if it persists. |
| 503 | (varies) | A dependency is briefly unavailable. Retry with backoff. |

## Validation errors

A `422` lists every failing field at once:

```json
{
  "error": {
    "type": "validation_error",
    "code": "invalid_fields",
    "message": "Validation failed for 1 field(s)",
    "details": [
      { "param": "items.0.unit_price.value", "message": "Input should be a valid integer" }
    ]
  },
  "request_id": "..."
}
```

## Codes you will meet at the till

| Code | HTTP | What to do |
|---|---|---|
| `qr_code_not_resolvable` | 404 | The QR code expired. Ask the member to refresh it in the app. |
| `membership_not_found` | 404 | Not a member of this brand. Continue without a member. |
| `external_transaction_id_conflict` | 409 | Two different receipts share an id. Fix the id. |
| `invalid_fields` | 422 | Fix the listed fields. |
| `invalid_token` | 401 | Get a new token, retry once. |
| `insufficient_scope` | 403 | Ask the brand to add the scope to your key. |
| `rate_limit_exceeded` | 429 | Wait `Retry-After` seconds. |

Authentication errors have their own page: see [Authentication](/guides/authentication/#errors).

New codes may be added. Handle unknown codes by their `error.type` and HTTP status.
