# Errors

A failure is a **problem document** (`application/problem+json`, RFC 9457) with an HTTP status and a stable `code`. Branch on `code` and `status`, never on the wording of `detail`.

| Field | Meaning |
| --- | --- |
| `type` | A link to the page that explains the code (the pages below). The link is the code’s address on docs.watchtower15.com, which answers with a permanent redirect to the code’s page on this site. |
| `title` | A short name of the problem. |
| `status` | The HTTP status, repeated. |
| `code` | The stable machine-readable code. |
| `detail` | A sentence about this occurrence. It may change; do not parse it. |
| `request_id` | The id of this call, also sent as the `X-Request-Id` header. Quote it to support. |
| `errors` | Only on `422`: the fields that were wrong, each with its messages. |
| `accepted_parameters` | No longer sent: it came only with `unknown_parameter`, which is retired. An unknown query parameter is now ignored. |
| `limit`, `used` | Only where a limit was reached: the limit and what was used. |

**A problem document (404 Not Found)**

```json
{
  "type": "https://docs.watchtower15.com/api/errors/not_found/",
  "title": "Not found",
  "status": 404,
  "code": "not_found",
  "detail": "No server with that id exists in this organisation.",
  "request_id": "9b2c1f4e-…"
}
```

**HTTP status code summary**

- 200 OK: It worked.
- 201 Created: A create worked.
- 202 Accepted: An action queued its work.
- 204 No Content: A delete worked.
- 401 Unauthorized: No valid key.
- 402 Payment Required: The organisation's billing blocks it.
- 403 Forbidden: The key may not do this.
- 404 Not Found: No such record or operation.
- 405 Method Not Allowed: Not with this method.
- 413 Content Too Large: A body over 1 MB.
- 422 Unprocessable Content: A parameter is wrong, or a limit is reached.
- 429 Too Many Requests: A rate or daily limit.
- 500 Internal Server Error: Our fault.
- 503 Service Unavailable: A data store did not answer in time.

## The error codes

| Code | Status |  |
| --- | --- | --- |
| [`invalid_request`](https://developer.watchtower15.com/api/errors/invalid_request/) | 422 | A parameter is missing or wrong. |
| [`unknown_parameter`](https://developer.watchtower15.com/api/errors/unknown_parameter/) | 422 | Retired, no longer sent: a query parameter the operation does not take is ignored. |
| [`not_found`](https://developer.watchtower15.com/api/errors/not_found/) | 404 | No such record or operation. |
| [`unauthorized`](https://developer.watchtower15.com/api/errors/unauthorized/) | 401 | The key is missing, malformed, expired or revoked. |
| [`forbidden`](https://developer.watchtower15.com/api/errors/forbidden/) | 403 | The key or its owner may not do this. |
| [`method_not_allowed`](https://developer.watchtower15.com/api/errors/method_not_allowed/) | 405 | The operation does not take this method. |
| [`rate_limited`](https://developer.watchtower15.com/api/errors/rate_limited/) | 429 | Too many calls too fast. |
| [`daily_limit_reached`](https://developer.watchtower15.com/api/errors/daily_limit_reached/) | 429 | The organisation’s daily call limit is used. |
| [`payment_required`](https://developer.watchtower15.com/api/errors/payment_required/) | 402 | The organisation has no valid payment method. |
| [`invoice_overdue`](https://developer.watchtower15.com/api/errors/invoice_overdue/) | 402 | An invoice is overdue. |
| [`limit_reached`](https://developer.watchtower15.com/api/errors/limit_reached/) | 422 | An account limit is reached. |
| [`held_limit_reached`](https://developer.watchtower15.com/api/errors/held_limit_reached/) | 422 | The limit on servers waiting for approval is reached. |
| [`store_unavailable`](https://developer.watchtower15.com/api/errors/store_unavailable/) | 503 | A data store did not answer in time. |
| [`internal_error`](https://developer.watchtower15.com/api/errors/internal_error/) | 500 | Something unexpected went wrong on our side. |

## Answers from the host, before the API runs

The host in front of the API answers a few things itself, so they do not carry the fields above:

-   A path outside `/v1/` is a `404` problem document with **no** `request_id` and no `X-Request-Id` — the call never reached the API.
-   A body over 1 MB is a `413`, and a `multipart` body a `415`. Both come back as a plain page, not a problem document. The API takes small JSON bodies only.

**Handling errors**

curl:

```bash
# -i prints the status line and the headers before the problem document.
curl -i https://rest.watchtower15.com/v1/servers/$SERVER_UUID \
  -H "Authorization: Bearer $WT15_API_KEY"
```

PHP:

```php
use WatchTower15\Exception\ApiException;
use WatchTower15\Exception\NotFoundException;
use WatchTower15\Exception\RateLimitException;

try {
    $server = $wt15->servers->view($serverUuid);
} catch (NotFoundException $e) {
    // 404 not_found: no such server in this organisation
} catch (RateLimitException $e) {
    sleep($e->getRetryAfter() ?? 60);
} catch (ApiException $e) {
    error_log($e->getErrorCode() . ' ' . $e->getRequestId());
}
```

Node.js:

```ts
import { APIError, NotFoundError, RateLimitError } from '@watchtower15/sdk';

try {
  const server = await wt15.servers.view(serverUuid);
} catch (error) {
  if (error instanceof NotFoundError) {
    // 404 not_found: no such server in this organisation
  } else if (error instanceof RateLimitError) {
    await new Promise((resolve) => setTimeout(resolve, (error.retryAfter ?? 60) * 1000));
  } else if (error instanceof APIError) {
    console.error(error.code, error.requestID);
  } else {
    throw error;
  }
}
```

Python:

```python
import time

from watchtower15 import APIError, NotFoundError, RateLimitError

try:
    server = client.servers.view(server_uuid)
except NotFoundError:
    pass  # 404 not_found: no such server in this organisation
except RateLimitError as error:
    time.sleep(error.retry_after or 60)
except APIError as error:
    print(error.code, error.request_id)
```

Source: https://developer.watchtower15.com/api/errors/
