# Errors

> What the bot API answers when something goes wrong. The error body, the errors of each field, the HTTP statuses and every JSON code Klang sends, with what it means in Klang.

## The error body

Every error is JSON with a numeric `code` and a `message`:

```json
{
  "code": 10003,
  "message": "Unknown channel."
}
```

- **`code`** is the number to check. It uses the numbers of Discord’s API, so the error classes
  of your library work. `0` is a general error with no more precise code.
- **`message`** is for people and can change. Don’t compare it in code.
- **`errors`** only comes with `50035`: see [Field errors](https://docs.klang.gg/bots/reference/errors/#field-errors).

A `429` has another body, with the wait: see [Rate limited](https://docs.klang.gg/bots/reference/errors/#rate-limited).

## Field errors

When a path parameter, the query string or the body doesn’t fit, Klang answers `400` with
`50035` and an `errors` object shaped like the input. Each problem sits under its field, in an
`_errors` list, with an inner `code` and a `message`. Items of an array go by their index, and
a problem with the input as a whole goes at the top.

```json
{
  "code": 50035,
  "message": "The request body isn’t valid.",
  "errors": {
    "message_reference": {
      "type": {
        "_errors": [{ "code": "BASE_TYPE_CHOICES", "message": "Only replies are supported." }]
      }
    }
  }
}
```

```json
{
  "code": 50035,
  "message": "The request body isn’t valid.",
  "errors": {
    "_errors": [{ "code": "INVALID", "message": "Only one of around, before and after." }]
  }
}
```

The inner codes:

| Inner code             | When                                                  |
| ---------------------- | ----------------------------------------------------- |
| `BASE_TYPE_REQUIRED`   | A required field is missing.                          |
| `BASE_TYPE_MAX_LENGTH` | A text or a list is too long.                         |
| `BASE_TYPE_MIN_LENGTH` | A text or a list is too short.                        |
| `NUMBER_TYPE_MAX`      | A number is too big.                                  |
| `NUMBER_TYPE_MIN`      | A number is too small.                                |
| `BASE_TYPE_CHOICES`    | The value isn’t one of the ones allowed.              |
| `INVALID`              | Anything else: a wrong type, a bad ID, a rule broken. |

## HTTP statuses

| Status | What it means                                                                                   |
| ------ | ----------------------------------------------------------------------------------------------- |
| `200`  | It worked, and the body has the result.                                                         |
| `204`  | It worked, and there’s nothing to return.                                                       |
| `400`  | The request is wrong. Fix it before sending it again.                                           |
| `401`  | The token is missing, wrong, reset or suspended. Stop: retrying won’t help.                     |
| `403`  | The bot isn’t allowed to do that here.                                                          |
| `404`  | The thing, or the route, doesn’t exist for the bot.                                             |
| `413`  | The body is too big.                                                                            |
| `429`  | Too many requests, or slow mode. Wait and retry.                                                |
| `500`  | Something failed in Klang. It answers `{ "code": 0, "message": "500: Internal Server Error" }`. |
| `503`  | Klang can’t serve the request right now (`130000`). Retry a little later.                       |

Answers `401`, `403` and `429` count towards the
[invalid requests limit](https://docs.klang.gg/bots/limits/#invalid-requests).

## Codes

These are the codes the bot API sends today. The [routes](https://docs.klang.gg/bots/reference/) list the ones each
can return.

| Code     | Status         | In Klang                                                                                                                                                                                                                    |
| -------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `0`      | Various        | A general error: a route that doesn’t exist (`404`, `"404: Not Found"`), a bad token (`401`, `"401: Unauthorized"`), a DM with itself (`400`), or a Klang error with no number in Discord’s list, with Klang’s own message. |
| `10003`  | `404`          | Unknown channel: it doesn’t exist, or the bot can’t see it.                                                                                                                                                                 |
| `10004`  | `404`          | Unknown server: it doesn’t exist, or the bot isn’t in it.                                                                                                                                                                   |
| `10007`  | `404`          | Unknown member: that user isn’t in the server.                                                                                                                                                                              |
| `10008`  | `404`          | Unknown message, or the message a reply points to is gone.                                                                                                                                                                  |
| `10011`  | `404`          | Unknown role: it isn’t one of the server’s.                                                                                                                                                                                 |
| `10013`  | `404`          | Unknown user, or one the bot shares nothing with.                                                                                                                                                                           |
| `10014`  | `400` or `404` | Unknown emoji: `400` when `{emoji}` can’t be read, `404` for a custom emoji the bot can’t use.                                                                                                                              |
| `10015`  | `404`          | Unknown webhook: it doesn’t exist, it’s in a channel the bot can’t see, or the token in its URL is wrong. With an interaction’s token: it’s wrong, older than 15 minutes, or the interaction has no first answer yet.       |
| `10026`  | `404`          | Unknown ban: that user isn’t banned.                                                                                                                                                                                        |
| `10060`  | `404`          | Unknown sticker, or one from a server the bot isn’t in.                                                                                                                                                                     |
| `10062`  | `404`          | Unknown interaction: a wrong ID or token, the 3 seconds to answer are over, or the bot left the server.                                                                                                                     |
| `10063`  | `404`          | Unknown command in that scope.                                                                                                                                                                                              |
| `10066`  | `404`          | The command has no permissions set in that server.                                                                                                                                                                          |
| `20001`  | `403`          | Bots can’t use this route: changing command permissions.                                                                                                                                                                    |
| `20012`  | `403`          | The route names another app than the token’s.                                                                                                                                                                               |
| `20016`  | `429`          | Slow mode. The body has `retry_after`.                                                                                                                                                                                      |
| `30003`  | `400`          | The channel already has 50 pinned messages.                                                                                                                                                                                 |
| `30007`  | `400`          | The channel already has 15 webhooks, or the server 50. Both limits answer this code.                                                                                                                                        |
| `30010`  | `400`          | The message already has 20 different reactions.                                                                                                                                                                             |
| `30008`  | `400`          | The server has as many emojis as its tier allows.                                                                                                                                                                           |
| `30032`  | `400`          | The scope already has 100 slash commands, or 15 user or message commands.                                                                                                                                                   |
| `30034`  | `400`          | 200 commands were created in that server today (UTC). A `400`, not a `429`: retrying won’t help until tomorrow.                                                                                                             |
| `30039`  | `400`          | The server has as many stickers as its tier allows.                                                                                                                                                                         |
| `40005`  | `413`          | A file, or all of a message’s files, is over the server’s plan limit, or the bot’s storage is full. See [Sending files](https://docs.klang.gg/bots/files/#limits). Also a sticker over 512 KB.                                                   |
| `40060`  | `400`          | The interaction was already answered.                                                                                                                                                                                       |
| `50001`  | `403`          | Missing access: the member list without the **Server members** intent switched on, or a server’s commands when the bot isn’t in that server.                                                                                |
| `50005`  | `403`          | Editing a message someone else sent.                                                                                                                                                                                        |
| `50006`  | `400`          | An empty message: no text, embed, sticker, component or file.                                                                                                                                                               |
| `50007`  | `403`          | The bot can’t send this person a direct message.                                                                                                                                                                            |
| `50013`  | `403`          | Missing permissions, or the bot is timed out. In moderation, also acting on someone not below the bot.                                                                                                                      |
| `50021`  | `400`          | Pinning a system message.                                                                                                                                                                                                   |
| `50034`  | `400`          | A bulk delete with a message 14 days old or older.                                                                                                                                                                          |
| `50035`  | `400`          | Invalid input, with `errors`.                                                                                                                                                                                               |
| `50109`  | `400`          | The body isn’t valid JSON.                                                                                                                                                                                                  |
| `130000` | `503`          | Klang is overloaded or one of its services is down.                                                                                                                                                                         |
| `160005` | `403`          | The thread is locked.                                                                                                                                                                                                       |
| `200000` | `400`          | The server’s AutoMod blocked the message.                                                                                                                                                                                   |
| `520001` | `400`          | The poll has ended.                                                                                                                                                                                                         |
| `520003` | `400`          | A poll’s message can’t be edited.                                                                                                                                                                                           |

A bot never gets an error for its own limit of servers: it’s the person adding it who’s told, and
the bot doesn’t join. With an unverified bot already in 100 servers, they read that it needs to be
verified first; see [Verify your bot](https://docs.klang.gg/bots/getting-started/#verify-your-bot).

For the cases where Klang answers differently from what a bot might expect, such as a hidden
channel answering `10003` instead of `50001`, see
[Differences from Discord’s API](https://docs.klang.gg/bots/differences/#errors).

## Rate limited

Going over a limit answers `429` without a `code`. `retry_after` is in seconds, with decimals,
and `global` says whether it was the global limit:

```json
{
  "message": "You’re sending requests too quickly.",
  "retry_after": 0.942,
  "global": false
}
```

The headers say the same: `Retry-After` (whole seconds, rounded up), `X-RateLimit-Scope`
(`user` or `global`) and, for the global limit, `X-RateLimit-Global: true`.

Slow mode is a `429` too, but with `code` `20016`:

```json
{
  "code": 20016,
  "message": "Slowmode is on in this channel. Wait a moment before sending again.",
  "retry_after": 30,
  "global": false
}
```

More in [Limits and rate limits](https://docs.klang.gg/bots/limits/#when-you-go-over).
