# Errors

> RFC 9457 problem details, every hoshime error code, and which errors to retry.

Errors are RFC 9457 problem details, with `Content-Type: application/problem+json`:

```json
{
  "type": "urn:hoshime:problem:invalid_parameter",
  "title": "Bad Request",
  "status": 400,
  "detail": "id is required.",
  "instance": "/v1/youtube/video",
  "code": "invalid_parameter",
  "requestId": "req_2Xb…",
  "errors": [{ "parameter": "id", "code": "required", "detail": "id is required" }]
}
```

Branch on `code`, which never changes for a given error. `detail` is written for people and may change. `errors` is present on `invalid_parameter` only. Quote `requestId` when you ask for help.

## Codes

| Code | Status | Meaning | Retry |
|---|---|---|---|
| `invalid_parameter` | 400 | A query parameter is missing, unknown or invalid. `errors[].code` is `required`, `unknown`, `type`, `range`, `oneof`, `max` or `format`. | No, fix the call |
| `auth_token_invalid` | 401 | The token is missing or not valid. | No |
| `auth_permission` | 403 | The token lacks the route's scope. | No |
| `auth_ip_not_allowed` | 403 | The token's IP allowlist does not hold the caller. | No |
| `not_found` | 404 | No such item at the source, it is not public, or no such endpoint. | No |
| `rate_limited` | 429 | Too many calls. Wait `Retry-After` seconds. | Yes |
| `upstream_error` | 502 | The source failed or answered an error. | Yes, later |
| `upstream_bad_response` | 502 | The source answered in a format hoshime cannot read. It is reported to us. | Later |
| `upstream_blocked` | 503 | The source refused every route hoshime tried. | Yes, later |
| `tunnel_unavailable` | 409 | The call's `tunnel` has no machine connected. | After `hoshime tunnel` reconnects |
| `proxy_unavailable` | 502 | A call with `profileId` alone could not use the profile's residential IP. | Yes, later, or pass `tunnel` or `proxy` |
| `profiles_unavailable` | 503 | Browser profiles cannot be opened right now. | Yes, later |
| `timeout` | 504 | The call took longer than 75 seconds. | Yes |
| `internal` | 500 | A hoshime bug. It is reported to us. | Later |

**Failures bill nothing.** Retrying a failed call never costs more than the one call that succeeds.

## A retry loop

```js
async function call(url, tries = 4) {
  for (let i = 0; ; i++) {
    const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.HOSHIME_TOKEN}` } });
    if (res.ok) return res.json();
    const problem = await res.json();
    const retry = ["rate_limited", "upstream_error", "upstream_blocked", "timeout"].includes(problem.code);
    if (!retry || i + 1 >= tries) throw Object.assign(new Error(problem.detail), problem);
    const wait = Number(res.headers.get("Retry-After")) || 2 ** i;
    await new Promise((r) => setTimeout(r, wait * 1000));
  }
}
```