# Errors

Every response that isn't a success has the same body: a kind to switch on, a message written for a person, and how long to wait when waiting helps.

## One shape

The body is always `{ error: { kind, message } }`. `kind` is a fixed set of values, so code can branch on it. `message` says the same thing in plain words and names the fix, so it can go straight onto a screen. `retryAfter` appears only on errors that waiting fixes.

Waiting fixes it 429 Too many requests

```json
{
  "error": {
    "kind": "rate_limited",
    "message": "You can check again in 10 seconds.",
    "retryAfter": 10
  }
}
```

The input needs changing 422 Unprocessable

```json
{
  "error": {
    "kind": "reserved_tld",
    "message": ".localhost is set aside and can't be registered, so there's no public DNS to check. Enter a domain you've registered."
  }
}
```

The HTTP status is the broad category and `kind` is the exact reason. Branch on the kind: several share a status, and 10 statuses cover all 26 kinds.

Handling an error

```javascript
const response = await fetch(url, options)
const data = await response.json()

if (!response.ok) {
  const { kind, message, retryAfter } = data.error
  switch (kind) {
    case "rate_limited":
    case "check_in_flight":
      // Waiting fixes these. retryAfter is in seconds.
      return retryIn(retryAfter)
    case "unauthorized":
      return startOver()
    default:
      // The message is written for a person. Show it as it is.
      return show(message)
  }
}
```

## What isn't an error

A check that doesn't find the record is a `200`. The request worked: we looked, and the answer is in `view.phase` and `view.outcome`. The same goes for a confirmation token that's already been used, and an unsubscribe pressed twice. Each answers `200` with a field that says what happened.

> **Errors are about the request, not the domain**
>
> If the request was understood and allowed, the status is 2xx. What we found in DNS is data, and [What each failure means](https://www.prufy.dev/docs/guides/failures.md) covers all of it.

## Headers

| Header | On | What it's for |
| --- | --- | --- |
| `x-request-id` | Every response | Names this request in our logs. Send your own, up to 64 letters, digits, hyphens or underscores, and we use it. Otherwise we make one that starts with `req_`. |
| `retry-after` | Errors with `retryAfter` | The same number of seconds as the body, where HTTP clients expect it. |
| `cache-control` | Every response | Always `no-store`. A claim changes between reads, so nothing should cache one. |

## Every kind

The first eight are about the domain that was typed, and only come from routes that take one. Each route's page lists the kinds it can answer.

| Kind | Status | What it means | What to do |
| --- | --- | --- | --- |
| `empty` | 422 | The domain was blank. | Ask for a domain. |
| `invalid_characters` | 422 | The domain has characters a domain can't contain. | Show the message. It names what to remove. |
| `label_too_long` | 422 | One part between dots is longer than 63 characters. | Show the message. |
| `too_long` | 422 | The whole name is longer than 253 characters. | Show the message. |
| `ip_address` | 422 | An IP address was sent where a domain belongs. | Ask for the domain name. |
| `reserved_tld` | 422 | The ending is set aside, like `.test` or `.localhost`, so there's no public DNS to check. | Ask for a registered domain. |
| `public_suffix` | 422 | The name is a suffix many people share, like `co.uk`. | Ask for the domain under it. |
| `single_label` | 422 | The name has no dot, so there's no ending to look up. | Ask for the full domain. |
| `unauthorized` | 401 | The request has no session, or one we don't recognise. | Create a claim to start a session, and send its cookie. |
| `not_found` | 404 | Nothing is at this address for you. Another account's claim, an expired setup link and a development route in production all look like this. | Check the id or token. Don't retry. |
| `method_not_allowed` | 405 | The address exists, but not with this method. | Use a method from the `Allow` header. Only `/api/v1` answers this way. |
| `bad_request` | 400 | The body isn't JSON, or doesn't have the shape the route expects. | Fix the request. `message` names the first field to fix, and `fields` lists each one. Don't retry it as it is. |
| `payload_too_large` | 413 | The body is bigger than any route takes. The limit is 16 KB. | Send only the fields the route lists. Don't retry the same body. |
| `rate_limited` | 429 | This has been done too often, too recently. | Wait `retryAfter` seconds, then try again. |
| `check_in_flight` | 409 | Another check of this claim is running right now. | Wait `retryAfter` seconds and read the claim. The other check's result will be there. |
| `not_checkable` | 409 | The claim is in a status where there's no record to look for or add. | Read the claim. `view.explanation.action` says what's possible. |
| `illegal_transition` | 409 | The claim's status doesn't allow this. A verified claim can't be rotated, for one. | Read the claim and offer what its status allows. |
| `version_conflict` | 409 | The claim changed while the request was running. | Read the claim again, then retry if it still makes sense. |
| `owner_conflict` | 409 | Another account proved this domain at the same moment, and theirs finished first. | Nothing. The claim stays pending and we check again on our own. |
| `domain_connect_disabled` | 404 | One-click setup is turned off on this server. | Show the record to add by hand. |
| `domain_connect_unavailable` | 409 | One-click setup isn't possible for this domain or in this status. | Show the record to add by hand. |
| `email_unavailable` | 503 | This server can't send email, or couldn't send to that address. | Offer the link to copy, or ask them to check the address. |
| `push_unavailable` | 503 | This server has no push keys set up. | Don't offer push. `capabilities.pushPublicKey` says so ahead of time. |
| `timed_out` | 503 | We didn't finish within 25 seconds. Anything already done, like a check, is kept. | Wait `retryAfter` seconds, then read the claim before trying again. |
| `unavailable` | 503 | Something every route needs, like the database, isn't answering. Only the health check says this. | Alert on it. Retry after `retryAfter` seconds. |
| `internal_error` | 500 | Something went wrong on our side. The details are in our logs, under the request id. | Try again in a moment. If it keeps happening, send us the `x-request-id`. |

---

Every page of these docs, as a list: https://www.prufy.dev/llms.txt
