# Errors

When a request fails, the Omono API answers with an HTTP status code and a
JSON body that explains what went wrong. This guide describes that body,
lists the error codes your integration should handle, and explains what to
do after a server error. Every code also has its own page, which the
`type` field of the error links to.

## What an error looks like

Every error response has the content type `application/problem+json` and a
body like this:

```json
{
  "type": "https://api.omono.co/docs/errors/invalid-reference",
  "title": "Invalid reference",
  "status": 422,
  "code": "invalid-reference",
  "detail": "A reference id in the body does not resolve …",
  "requestId": "req_9Jc4TqX2mBv7RkLpW",
  "errors": [
    { "field": "lineItems[1].stockItemId", "message": "is not sellable" }
  ]
}
```

| Field       | What it tells you                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type`      | A link to the page for this code, which explains what it means and what to do. The link always points to the page on `api.omono.co`, whichever platform sent the error.                                                                                                                                                                                                                                                                                 |
| `title`     | A short summary of the error, written for a person to read.                                                                                                                                                                                                                                                                                                                                                                                             |
| `status`    | The HTTP status code of the response.                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `code`      | A short code that identifies the error. Handle errors by this field, not by the text, which can change at any time.                                                                                                                                                                                                                                                                                                                                     |
| `detail`    | A sentence about this failure, written for a person to read. When a value breaks one of Omono's rules, it names the field, for example `lineItemOrder[2] appears more than once.` When the body doesn't match the request's schema, it describes the code in general, and `errors[]` says which field is at fault.                                                                                                                                      |
| `requestId` | The ID of this request. Quote it when you ask Omono for help.                                                                                                                                                                                                                                                                                                                                                                                           |
| `errors`    | A list of the fields at fault. Each entry has a `field` and a `message`. The `field` is usually the path to a field in your body, such as `lineItems[1].stockItemId`, but it can also name a query parameter, such as `cursor`, or a header, such as `Idempotency-Key`. The list comes with every `422` error, and with some `404` and `409` errors, such as an error about a line item, about `lineItemOrder`, or about a field of an archived record. |

Handle errors by their `code` and, when you need to know which field is at
fault, by `errors[].field`. Never match on the `title`, the `detail` or an
`errors[].message`: they're written for people, and Omono can reword them at
any time.

Omono usually reports only the first problem it finds, so `errors` often
holds a single entry. Fix that field and send the request again, and a later
check can then name another field. A `missing-required-field` error is
different: its `errors` lists every missing field that Omono found at that
stage. Omono checks a request in two stages. First it checks the request's
shape against the schema in this reference. Then it checks the rules that
depend on your organisation's data and settings, such as a field that your
settings make required. So when you add the fields that the first error
lists, a second error can still list fields that the rules require. When
the problem is with one entry of
the `lineItems` array you sent, `field` gives that entry's position in your
array, counting from 0, such as `lineItems[2]` or `lineItems[2].quantity`.
That's true for an entry that deletes a line item, too.

Omono can add new codes at any time, for example when it adds a new rule
that refuses a request because of the state of a record. So handle a code
you don't recognise by its status: a `4xx` status means the request needs to
change, and a `5xx` status means something went wrong on Omono's side. Once
the API is generally available, Omono won't remove or rename a code that a
stable operation returns within version 1. Until then, and on a beta
operation at any time, a code can change, and the
[changelog](/docs/changelog) lists the change, as the
[compatibility policy](/docs/compatibility) explains.

Some errors carry extra fields of their own. For example, `events-expired`
includes `latestSequence`, a `PATCH` with `lineItems` that fails part-way
includes `appliedLineItems`, and a failed create that leaves a draft behind
names it in `keptDocument`.

### Request IDs

The `requestId` is also in the `X-Request-Id` header of every response. You
can send your own ID for a request in an `X-Request-Id` header, too, for
example to match Omono's records with your own logs. It can be 1 to 64
characters long, using the letters A to Z and a to z, the digits 0 to 9,
and `.`, `_`, `:` and `-`. Omono logs it next to its own ID and returns it
in the `X-Client-Request-Id` header. If your value doesn't follow those
rules, Omono ignores it.

Never put an API key in `X-Request-Id`. If the header contains a key, the
request fails with a `400` error and the code `malformed-request`, and
Omono doesn't store the header's value or send it back.

## Codes your integration should handle

For most codes, it's enough to log the error and show its `detail` to a
person. The codes below need their own handling in your integration.

| Status | Code                                                                                                                                                                          | What to do                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 429    | [`rate-limited`](/docs/errors/rate-limited)                                                                                                                                   | Wait for the number of seconds in the `Retry-After` header, or 60 seconds if there isn't one. Then send the same request again, with the same `Idempotency-Key`.                                                                                                                                                                                                                                                                              |
| 429    | [`upload-limit-reached`](/docs/errors/upload-limit-reached)                                                                                                                   | Your organisation has uploaded its allowance of files for today, in UTC. Don't send the upload again until the time in `Retry-After`, which is 00:00 UTC. Your other requests still work.                                                                                                                                                                                                                                                     |
| 503    | [`temporarily-unavailable`](/docs/errors/temporarily-unavailable)                                                                                                             | Wait and retry, in the same way as for `rate-limited`. If `detail` tells you to read the record again, do that first, and retry with its current `ETag`.                                                                                                                                                                                                                                                                                      |
| 503    | [`request-timeout`](/docs/errors/request-timeout)                                                                                                                             | Wait and retry, in the same way as for `rate-limited`.                                                                                                                                                                                                                                                                                                                                                                                        |
| 500    | [`internal-error`](/docs/errors/internal-error)                                                                                                                               | Retry once with the same `Idempotency-Key`. Some requests need a check first, as [After a 500 error](/docs/errors#after-a-500) explains.                                                                                                                                                                                                                                                                                                                  |
| 412    | [`precondition-failed`](/docs/errors/precondition-failed)                                                                                                                     | The record has changed since you read it. Read it again, apply your change again, and retry with the new `ETag`.                                                                                                                                                                                                                                                                                                                              |
| 409    | [`idempotency-in-progress`](/docs/errors/idempotency-in-progress)                                                                                                             | A request with the same `Idempotency-Key` is still running. Wait for the number of seconds in `Retry-After`, then retry.                                                                                                                                                                                                                                                                                                                      |
| 409    | [`dispatch-state-changed`](/docs/errors/dispatch-state-changed)                                                                                                               | Someone moved the dispatch at the same time as you. Read it again and decide what to do.                                                                                                                                                                                                                                                                                                                                                      |
| 422    | [`validation-failed`](/docs/errors/validation-failed), [`invalid-reference`](/docs/errors/invalid-reference), [`missing-required-field`](/docs/errors/missing-required-field) | Your data is wrong, and `errors[].field` names the field at fault. `missing-required-field` means you left out a field that's required, either always, as the reference marks it, or because of your organisation's settings or the record's state. `validation-failed` means a value isn't valid, and `invalid-reference` means an ID can't be used. Fix the data. Retrying the same request won't help.                                     |
| 400    | [`malformed-request`](/docs/errors/malformed-request)                                                                                                                         | Your integration has a bug. For example, the body isn't valid JSON, or the query has a parameter the operation doesn't accept. `detail` says what's wrong. Fix the request.                                                                                                                                                                                                                                                                   |
| 428    | [`precondition-required`](/docs/errors/precondition-required)                                                                                                                 | Your integration has a bug. It sent a `DELETE`, or a `PATCH` with `lineItems` or `lineItemOrder`, but no `If-Match` header. Fix the request. For a dispatch or a delivery, send the `ETag` of its sales order or purchase order.                                                                                                                                                                                                              |
| 409    | [`immutable-field`](/docs/errors/immutable-field)                                                                                                                             | Your integration has a bug. It tried to change a field that can't change once the record exists: a line item's `stockItemId`, or a stock item's `kind`. To use another stock item on a document, delete the line item and add a new one. To change a stock item's `kind`, create a new stock item.                                                                                                                                            |
| 409    | [`idempotency-key-reuse`](/docs/errors/idempotency-key-reuse)                                                                                                                 | Your integration has a bug. It sent an `Idempotency-Key` again to the same operation, but with a different body or for a different record. Use a new key for each new request, and the same key only to retry the same request.                                                                                                                                                                                                               |
| 409    | [`create-incomplete`](/docs/errors/create-incomplete)                                                                                                                         | An earlier request with the same key stopped part-way through a create, and someone has since changed the Draft it left. Read the Draft that `keptDocument` names, and [finish it](/docs/writing#if-a-create-fails) or leave it to your staff.                                                                                                                                                                                                |
| 409    | [`document-too-large`](/docs/errors/document-too-large)                                                                                                                       | The record has more than 10,000 related records, or more than 32 MB of JSON with them. Don't retry. Ask for fewer related records with `include`, or read it in Omono, as [Include related records](/docs/reading#include) explains. A write that would return too many related records, or related records that are too large, changes nothing, so make it in Omono, as [Very large documents](/docs/writing#very-large-documents) explains. |
| 401    | [`unauthenticated`](/docs/errors/unauthenticated)                                                                                                                             | The key is missing, wrong, expired or revoked. Check that you send the right key in the `Authorization` header. If the key has expired or been revoked, an admin of your organisation creates a new one on the **API** page of **Settings** in Omono.                                                                                                                                                                                         |
| 403    | [`insufficient-scope`](/docs/errors/insufficient-scope)                                                                                                                       | The key doesn't have the scope this operation needs. An admin adds the scope that `detail` names to the key on Settings → API.                                                                                                                                                                                                                                                                                                                |
| 403    | [`api-access-disabled`](/docs/errors/api-access-disabled)                                                                                                                     | API access isn't on for your organisation. If an admin switched it off, they can switch it back on from the **API** page of **Settings**. If `detail` says the API isn't switched on yet, or has been suspended, the admin asks Omono instead.                                                                                                                                                                                                |
| 404    | [`not-found`](/docs/errors/not-found)                                                                                                                                         | No record with that ID exists in your organisation. Remove your copy of a record only when a `GET` of that record by its own ID, such as `GET /sales-orders/{id}`, returns this code. Other requests can return it while the record still exists. For example, `GET /documents/{id}/content` returns it for an archived document.                                                                                                             |
| 404    | [`route-not-found`](/docs/errors/route-not-found)                                                                                                                             | Your integration has a bug: no operation has that path. Check the method and path against the API reference. Paths are case-sensitive and have no trailing slash. This code says nothing about any record, so never remove your copy of a record because of it.                                                                                                                                                                               |
| 410    | [`events-expired`](/docs/errors/events-expired)                                                                                                                               | Load everything again, then continue reading events from the `latestSequence` in the response. [Find deleted records](/docs/syncing#deletes) explains how.                                                                                                                                                                                                                                                                                    |

## HTTP status codes

The API uses standard HTTP status codes. A `2xx` status means the request
worked, and a `4xx` or `5xx` status means it failed. A `302` from
`GET /documents/{id}/content` and a `304` from a
[conditional read](/docs/reading#conditional-reads) also mean the request
worked. Each operation in the [API reference](/docs) lists the errors it can
return. Any request can also fail with a `404` error and the code
`route-not-found` when no operation has its path, or with a `405` error
when the path doesn't support its method.

| Status | What it means                                                                                                                                                                                                                                             |
| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 400    | The request is malformed. The list below this table gives every cause, and `detail` says which one applies.                                                                                                                                               |
| 401    | The key is missing, wrong, expired or revoked. For a key that Omono recognises, `detail` says which.                                                                                                                                                      |
| 403    | The key doesn't have the scope it needs, or API access isn't on for your organisation.                                                                                                                                                                    |
| 404    | There's no record with that ID in your organisation, and the code is `not-found`. An ID that belongs to another organisation gets the same answer. A path that the API doesn't have gets the code `route-not-found` instead.                              |
| 405    | The path doesn't support that HTTP method, and the code is `method-not-allowed`. An `OPTIONS` request with a valid key gets this error too. A browser's preflight request carries no key, so it gets a `401` error.                                       |
| 409    | The request conflicts with the current state of a record, for example because the record is archived or a dispatch has already shipped, or the record is too large to return. The `code` says how. The same request fails again until that state changes. |
| 410    | Your saved events sequence can't be used any more, because it's too old or comes from before a sandbox reset or a restore.                                                                                                                                |
| 412    | The `If-Match` header doesn't match the record's current `ETag`.                                                                                                                                                                                          |
| 413    | The body is too large, and the code is `payload-too-large`. The limit is 1 MB for JSON, measured after decompression, and 20 MB for an uploaded file.                                                                                                     |
| 415    | The content type or encoding isn't supported, and the code is `unsupported-media-type`. Send JSON as `application/json` in UTF-8, compressed with `gzip`, `deflate` or `br` if you like, and uploads uncompressed.                                        |
| 422    | The request failed validation, and `errors` names the field at fault.                                                                                                                                                                                     |
| 428    | A `DELETE`, or a `PATCH` with `lineItems` or `lineItemOrder`, was sent without an `If-Match` header.                                                                                                                                                      |
| 429    | You've reached a rate limit, or your organisation's daily allowance for uploads or for stored line item descriptions. `Retry-After` tells you how long to wait.                                                                                           |
| 500    | Something went wrong on Omono's side. Quote the `requestId` if you contact Omono.                                                                                                                                                                         |
| 503    | The API is temporarily unavailable or too busy, Omono couldn't reach its database during a read, or the request took too long. `Retry-After` tells you how long to wait.                                                                                  |

A request is malformed, and fails with a `400` error and the code
`malformed-request`, in any of these cases:

- The body isn't valid JSON, or it nests objects and arrays more than 32
  levels deep.
- The body doesn't match its `Content-Length` header, or it can't be
  decompressed with the encoding its `Content-Encoding` header names.
- The query has a parameter the operation doesn't accept, or a parameter
  whose name has square brackets, such as `ids[0]`. Send a list as one
  comma-separated value instead, for example `ids=a,b`, or repeat the
  parameter, as in `ids=a&ids=b`.
- The query has a parameter that takes one value more than once, such as
  `limit=10&limit=20`.
- The path isn't valid percent-encoding.
- The `X-Request-Id` header contains an API key.
- An upload's `Content-Type` has no usable boundary, or its
  `multipart/form-data` body can't be read.

## After a 500 error

A `500` error means something went wrong on Omono's side, and the request
may have stopped part-way through. What to do next depends on what the
request was doing:

| If the request was                                                                  | What to do                                                                                                                                                                                                                                                         |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Creating a quote, sales order, credit note or purchase order                        | Omono usually cleans up after itself, so retry with the same `Idempotency-Key`. If the error names a document in `keptDocument`, don't retry: finish that document instead. [Recover from a failed create](/docs/writing#if-a-create-fails) explains what happens. |
| Creating anything else                                                              | The record may have been saved. Before you retry, list the resource with `created_gte` to check.                                                                                                                                                                   |
| A `PATCH` with `lineItems`                                                          | If the document had already changed, the error includes `appliedLineItems`, which lists the line items that changed. The lists can be empty when only the document's other fields changed. Read the document again after any error on this request.                |
| Moving a dispatch to the final status of your workflow, which ships and invoices it | Read [Finish a ship that failed](/docs/workflow#if-a-ship-fails).                                                                                                                                                                                                  |

If Omono couldn't reach its database while it handled a write, the error's
`detail` says so, and the write may have been saved in part. Omono may not
have been able to clean up, so check the record before you retry, whatever
the request was doing. A read that can't reach the database fails with a
`503` error and the code `temporarily-unavailable` instead, and changes
nothing.

If the error keeps happening, contact Omono with the `requestId`.

## Errors from the network edge

Some error responses come from the network edge, which means the services
in front of the API, such as its firewall and load balancer, and not from
the API itself. They don't
have the usual error body:

| Response                                                           | What it looks like                                                                                                                   |
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| A firewall `403`, or an error from `520` to `526`                  | An HTML page with a `cf-ray` header, and no `requestId`. If you need help, send the `cf-ray` value to Omono support within 24 hours. |
| A `429` from the edge, when one IP address sends too many requests | JSON, but without a `requestId`.                                                                                                     |
| A `503` from the load balancer while the API is out of service     | A body that isn't `application/problem+json`.                                                                                        |

Retry `429` and `503` errors, and errors from `520` to `526`, with backoff. Follow `Retry-After` when
it's there, and wait 60 seconds when it isn't.

## Planned maintenance

When Omono deploys a new version, the API is unavailable for about 3
minutes. During that time, every request fails with a `503` error. At
first the error has the code `temporarily-unavailable` and a `Retry-After`
header. Then, until the new version has started, the load balancer
answers instead, without the usual error body and without `Retry-After`,
as the table above shows. Omono doesn't announce deploys in advance.

So treat a `503` as temporary. Keep retrying with backoff for at least 5
minutes, which is longer than a deploy takes, before your integration
reports the API as down or gives up on its work. Follow `Retry-After` when
it's there, and wait 60 seconds when it isn't. Retry a write with the same
`Idempotency-Key`, so that it runs only once.
