
# Errors

Every error has one shape:

```json
{
  "error": {
    "code": "invalid_request",
    "message": "The request body is invalid.",
    "details": { "issues": [{ "path": "name", "message": "Required" }] }
  }
}
```

- `code` is stable and machine-readable. Branch on it, not on the message.
- `message` is a sentence a person can act on. It is safe to show.
- `details` is there when there is more to say: `issues` or `fields` (keyed by the path of the value) for validation errors, `errors` and `warnings` for an invalid page or SiteSpec, `problems` when a workflow can't be published, and `scope` for a missing scope.

## Codes

| Code | Status | Means | Do |
|---|---|---|---|
| `bad_request` | 400 | The request is malformed, for example not JSON. | Fix the request. |
| `confirmation_required` | 400 | Deleting a site needs `?confirm=<siteId>`. | Add the confirmation. |
| `unauthorized` | 401 | No key, or the key isn't valid or was revoked. | Check the `Authorization` header. |
| `forbidden` | 403 | The key or the account's role can't do this. | Use an owner's or admin's key. |
| `insufficient_scope` | 403 | The key lacks the scope; `details.scope` names it. | Make a key with that scope. |
| `not_found` | 404 | Not found, or not reachable with this key. | Check the id and the key's sites. |
| `conflict` | 409 | It clashes with the current state: a taken hostname, a read-only synced collection, a request still in progress. | Read the message; retry later for in-progress. |
| `gone` | 410 | Already deleted. | Nothing to do. |
| `precondition_failed` | 412 | The page changed since you read it. | Read it again, then change it. See [ETags](https://developers.unvop.com/guides/etags.md). |
| `too_large` | 413 | The file or body is too large. | Send less. |
| `unsupported_media_type` | 415 | That file type isn't supported. | See [Media](https://developers.unvop.com/guides/media.md) for the types. |
| `invalid_request` | 422 | The body is invalid; `details` says which values. | Fix the values named. |
| `invalid_spec` | 422 | A page or SiteSpec has problems; `details.errors` lists them by path. | Fix each path. |
| `idempotency_mismatch` | 422 | The Idempotency-Key was used before with a different body. | Use a new key. See [Idempotency](https://developers.unvop.com/guides/idempotency.md). |
| `rate_limited` | 429 | Too many requests. | Wait `Retry-After` seconds. See [Rate limits](https://developers.unvop.com/guides/rate-limits.md). |
| `internal_error` | 500 | Something went wrong on our side. | Retry with backoff. |

## Handle them

```js tab="JavaScript"
const res = await fetch(url, { headers });
if (!res.ok) {
  const { error } = await res.json();
  if (error.code === 'precondition_failed') {
    // Someone changed the page: read it again and reapply your change.
  } else if (error.code === 'rate_limited') {
    await new Promise((r) => setTimeout(r, Number(res.headers.get('Retry-After')) * 1000));
  } else {
    throw new Error(`${res.status} ${error.code}: ${error.message}`);
  }
}
```

```python tab="Python"
res = requests.get(url, headers=headers)
if not res.ok:
    error = res.json()["error"]
    if error["code"] == "precondition_failed":
        pass  # Someone changed the page: read it again and reapply your change.
    elif error["code"] == "rate_limited":
        time.sleep(int(res.headers["Retry-After"]))
    else:
        raise RuntimeError(f"{res.status_code} {error['code']}: {error['message']}")
```
