# Errors, retries and idempotency

> The error envelope, the codes, retries and what is never counted.

## The error envelope

Every error has the same shape:

```json
{ "errors": [ { "code": "feature_not_enabled", "message": "Mastering is not switched on for this account.", "feature": "mastering" } ] }
```

Branch on `code`, never on `message` (messages are written for people and may change). Some errors carry more
fields: `field` names what was wrong in your request, and limits carry `limit` and `used`.

| Status | What it means | What to do |
|---|---|---|
| 422 | Something in the request is wrong. | Fix it; `field` says where. Sending it again unchanged gives the same answer. |
| 401 | We do not know who you are. | Check the key or the session. |
| 402 | The account cannot do this now. | Write to us, or use a test key. |
| 403 | You may not do this. | The code says why. |
| 404 | Not there, or not yours (we never say which). | Check the id, and that the key is of the same kind, test or live. |
| 409 | It is not in the right state. | Read it again, then decide. |
| 429 | Too many requests. | Wait `Retry-After` seconds. |
| 5xx | Our side. | Retry with backoff and the same `Idempotency-Key`. |

## Every code

### Who you are and what you may do

| Code | Status | Meaning |
|---|---|---|
| `authentication_failed` | 401 | The key is missing, unknown, revoked or has ended. |
| `session_expired` | 401 | The embedded builder's session has ended. Make a new one on your server. |
| `account_closed` | 403 | The account is closed, so none of its keys work. Contact WAVBEE if you think that is wrong. |
| `account_not_live` | 402 | Live keys work only while the account is live and its payments are in order. Test keys still work. |
| `quota_exceeded` | 402 | The account has used what was agreed for this period; `limit` and `used` say how much. |
| `feature_not_enabled` | 403 | Not switched on for your account; `feature` says which. |
| `capability_required` | 403 | A person in your organisation made this call, and their role does not allow it. |
| `secret_key_required` | 403 | The call needs a secret key from your server, not a console or embed session. |
| `impersonation_not_allowed` | 403 | WAVBEE staff helping in your console cannot change your webhooks or their secrets for you. |
| `invalid_origin` | 403, 422 | The call came from, or the session asked for, a site that is not on your list of sites. |
| `rate_limited` | 429 | Too many requests. Wait `Retry-After` seconds. |
| `not_found` | 404 | Not there, or not yours. |

### The request

| Code | Status | Meaning |
|---|---|---|
| `validation_error` | 422 | A field is missing or wrong; `field` says which. |
| `idempotency_key_reused` | 422 | The `Idempotency-Key` was used before with a different body. |
| `config_hard_floor` | 422 | `config` asks for less than the rules allow, or is malformed; `field` names the setting. |
| `invalid_patch` | 422 | A patch is malformed, or changes a path that cannot be changed. |
| `invalid_answer` | 422 | The answer is not one of the question's options, or its value is missing or wrong. |
| `not_answerable` | 409 | The question has been answered, has ended or has been replaced. |
| `track_count` | 422 | Too few or too many tracks. |
| `audio_required` | 422 | A track has no audio. |
| `matching_result_invalid` | 422 | A matching result sent with a track could not be used as it is; `field` says which. |
| `matching_result_not_allowed` | 422 | Your account does not send its own matching results, or the call did not come from your server. |
| `import_failed` | 422 | The release could not be read from where it was asked for. |

### Drafts and embed sessions

| Code | Status | Meaning |
|---|---|---|
| `draft_conflict` | 409 | The draft changed since you read it. Read it again and re-apply your change. |
| `draft_not_editable` | 409 | The draft has been submitted or archived. |
| `duplicate_external_ref` | 409 | Another draft has this `external_ref`. |
| `embed_not_configured` | 422 | No site is on your list of sites yet. Add one under Developers → Embed. |

### Files and fixes

| Code | Status | Meaning |
|---|---|---|
| `unsupported_media_type` | 422 | Not a kind of file we accept, or not what it says it is. |
| `file_too_large` | 422 | Over the size limit. |
| `direct_upload_unavailable` | 501 | Direct upload is not offered here. Send the file through the ordinary upload. |
| `upload_incomplete` | 422 | The file did not arrive. Start the upload again. |
| `upload_mismatch` | 422 | The file that arrived is not the one described. Start the upload again. |
| `not_measured` | 409 | The audio has not been measured yet. Ask again in a few seconds. |
| `not_read` | 409 | The cover has not been read yet. Ask again in a few seconds. |
| `nothing_to_fix` | 422 | There is nothing on this file we can fix. |
| `nothing_to_trim` | 422 | There is no long silence to trim. |
| `nothing_to_remove` | 422 | There is nothing on the cover to remove. |
| `unknown_region` | 422 | The marked place is not one found on this cover. |

### Webhooks

| Code | Status | Meaning |
|---|---|---|
| `endpoint_disabled` | 422 | The endpoint is switched off. |
| `event_not_enabled` | 422 | The endpoint does not listen to this event. |
| `no_webhook_endpoints` | 422 | No endpoint listens to this event, so there is nothing to send again. |

### Mastering and stems

| Code | Status | Meaning |
|---|---|---|
| `session_not_ready` | 409 | The file is still being measured. |
| `render_not_finished` | 409 | The master is not finished. |
| `stems_not_finished` | 409 | The stems are not finished. |
| `stems_unavailable` | 503 | Stem separation is not available now. |
| `profile_unavailable` | 422 | There is no such profile, or too few tracks behind it. |
| `experiment_off` | 422 | The setting is not switched on here. |
| `duplicate_reference` | 409 | Another reference track has this `external_ref`. |

### The assistant

| Code | Status | Meaning |
|---|---|---|
| `assistant_unavailable` | 503 | The assistant cannot answer now. The questions and fields work without it. |

## Idempotency

Every request that creates something needs an `Idempotency-Key` header: a unique string per logical operation, for
example a UUID.

- Retrying with the **same key and the same body** returns the first answer, without doing the work twice and
  without counting it twice.
- The **same key with a different body** is refused with `422 idempotency_key_reused`.

So when a request times out, retry it with the same key. Use a new key for every new operation: a key stays
tied to the first request made with it.

## What is never counted

These never count in your usage: a test key, a replayed request, a run that could not reach a verdict, and `POST /v1/qc/validate`.
