# Errors

> Every error has an HTTP status, a fixed code for your program and a message for people.

Source: https://findort.ch/en/docs/errors/

## Format

Errors always come as JSON in this shape:

```json
  "error": {
    "code": "invalid_request",
    "message": "limit must be a number between 1 and 20"
  }
}
```

| Field | Description |
|---|---|
| `code` | Fixed code. This is what your program reacts to. |
| `message` | Explanation for developers, in English. The wording may change, do not parse it. |

## All codes

| HTTP | Code | Meaning | What to do |
|---|---|---|---|
| 400 | `invalid_request` | A parameter is missing or invalid. The message names it, like `q is required` or `Unknown type "house"`. | Fix the request, do not retry |
| 400 | `secret_key_in_url` | Secret key `sk_` sent as `?key=` in the URL | Send it as `Authorization: Bearer <key>` and rotate it, it may be in a log |
| 401 | `missing_key` | No key was sent | Send `Authorization: Bearer <key>` (public keys also as `?key=`) |
| 401 | `invalid_key` | We do not know this key, or it was deleted | Check the key in the dashboard |
| 403 | `origin_not_allowed` | A public key used from an origin that is not listed, or with no origin at all (for example from a server) | Add the origin to the key, or use an `sk_` key on servers |
| 404 | `not_found` | This path does not exist | Check the URL: `/v1/search` or `/v1/reverse` |
| 405 | `method_not_allowed` | A method other than `GET` | Use `GET` |
| 429 | `rate_limited` | Too many requests per minute | Wait `Retry-After` seconds, then retry |
| 429 | `quota_exceeded` | The monthly quota (Free) or your budget for extra requests (paid plans) is used up | Raise the budget, change plans or wait for the next month. Retrying does not help. |
| 500 | `internal_error` | Unexpected error on our side | Try again later |
| 502 | `upstream_error` | The search index is unreachable or did not answer within 8 seconds | Retry after a pause |

## Retry or not

- **4xx except 429:** the request itself is wrong. Retrying gives the same result.
- **429 `rate_limited`:** wait the seconds from `Retry-After`. The Web Component and `autocomplete()` only send the latest request while someone types anyway.
- **429 `quota_exceeded`:** do not retry. Show your users a plain text field so the form keeps working.
- **5xx:** retry with growing pauses, for example after 1, 2 and 4 seconds.

## In the libraries

All libraries throw their own exception with HTTP status and code when the API returns an error.

```js
import { Findort, FindortError } from 'findort';

try {
  await findort.search('Bern');
} catch (e) {
  if (e instanceof FindortError && e.code === 'quota_exceeded') showPlainInput();
  else throw e;
}
```

```php
    $findort->search('Bern');
} catch (Findort\FindortException $e) {
    echo $e->getCode();     // 429 (HTTP status)
    echo $e->errorCode;     // quota_exceeded
    echo $e->getMessage();
}
```

```python

try:
    findort.search("Bern")
except FindortError as e:
    print(e.status, e.code, e)  # 429 quota_exceeded ...
```

Extra codes from the libraries:

| Code | When |
|---|---|
| `network_error` | PHP and Python: server unreachable, status `0` |
| `http_error` | A response without readable JSON, for example from a proxy. The message contains the HTTP status. |

In JavaScript, network errors and timeouts throw the error from `fetch` (`TypeError`, `AbortError` or `TimeoutError`), not `FindortError`.

## In the Web Component

`<findort-search>` fires a `findort-error` event on errors. `e.detail.error` is the error from above.

```js
  console.warn(e.detail.error.code, e.detail.error.message);
});
```
