# Errors

Public error classes and broker protocol codes

Package: @cookiemonsterdev/kafka-core 2.8.1
Canonical: https://cookiemonsterdev.github.io/kafka/docs/core/reference/errors/
Source revision: b112a684d2b3cdc7deadb3513041fcdbdc1c18b5

Guide: [Errors](https://cookiemonsterdev.github.io/kafka/docs/core/guides/errors/). Source:
[`errors.ts`](https://github.com/cookieMonsterDev/kafka/blob/master/packages/core/src/errors.ts).
Broker codes:
[`protocol/error-codes.ts`](https://github.com/cookieMonsterDev/kafka/blob/master/packages/core/src/protocol/error-codes.ts)
and the [protocol guide](https://kafka.apache.org/43/design/protocol/).

`isKafkaError` exists in `errors.ts` but is **not** on the public barrel
(`src/index.ts`). Catch with `instanceof`.

## Hierarchy

```
Error
├── KafkaError                    retriable defaults to true
│   ├── KafkaNonRetriableError    retriable is always false
│   │   ├── KafkaServerDoesNotSupportApiKey
│   │   ├── KafkaSASLAuthenticationError
│   │   ├── KafkaPartialMessageError
│   │   ├── KafkaGroupCoordinatorNotFound
│   │   ├── KafkaNotImplemented
│   │   ├── KafkaTimeout → KafkaLockTimeout, KafkaDeliveryTimeoutError
│   │   ├── KafkaMessageTooLargeError
│   │   ├── KafkaUnsupportedMagicByteInMessageSet
│   │   ├── KafkaCorruptRecordError    expectedCrc, computedCrc
│   │   ├── KafkaInvariantViolation
│   │   ├── KafkaInvalidVarIntError / KafkaInvalidLongError
│   │   └── KafkaNumberOfRetriesExceeded
│   ├── KafkaProtocolError        type, code, topic?, partition?
│   │   ├── KafkaOffsetOutOfRange
│   │   ├── KafkaMemberIdRequired
│   │   ├── KafkaCreateTopicError
│   │   ├── KafkaAlterPartitionReassignmentsError
│   │   └── KafkaUpdateFeaturesError
│   ├── KafkaConnectionError → KafkaConnectionClosedError
│   ├── KafkaRequestTimeoutError
│   ├── KafkaMetadataNotLoaded → KafkaTopicMetadataNotLoaded
│   ├── KafkaStaleTopicMetadataAssignment
│   ├── KafkaDeleteGroupsError
│   ├── KafkaBrokerNotFound
│   ├── KafkaDeleteTopicRecordsError
│   └── KafkaNoBrokerAvailableError
├── KafkaAggregateError           errors: readonly unknown[]
└── KafkaFetcherRebalanceError
```

Every `KafkaError` has `name`, `retriable`, optional `helpUrl`, and `cause`.

## When they fire

| Class                             | Typical cause                                                            |
| --------------------------------- | ------------------------------------------------------------------------ |
| `KafkaProtocolError`              | Broker returned an error code                                            |
| `KafkaOffsetOutOfRange`           | Fetch offset not in log                                                  |
| `KafkaConnectionError`            | Socket / broker unreachable                                              |
| `KafkaRequestTimeoutError`        | In-flight request exceeded `requestTimeout`                              |
| `KafkaSASLAuthenticationError`    | SASL handshake failed                                                    |
| `KafkaServerDoesNotSupportApiKey` | Broker `ApiVersions` has no overlap for a used API                       |
| `KafkaNumberOfRetriesExceeded`    | Retrier exhausted (`retryCount`, `retryTime`)                            |
| `KafkaDeliveryTimeoutError`       | Producer `deliveryTimeoutMs` elapsed before a `send`/`sendBatch` settled |
| `KafkaMessageTooLargeError`       | A record, or a call's records combined, exceeded `maxRequestSize`        |
| `KafkaCreateTopicError`           | CreateTopics failed for that topic name                                  |
| `KafkaUpdateFeaturesError`        | UpdateFeatures failed for that feature name                              |
| `KafkaNoBrokerAvailableError`     | Pool has no connected broker                                             |
| `KafkaCorruptRecordError`         | Fetched batch's CRC didn't match its bytes (`checkCrcs: true`, default)  |

## Protocol codes (common)

`KafkaProtocolError.type` / `.code` match these entries. `retriable` on the
class follows the table.

| Code | Type                         | Retriable | Meaning                                                                                                                             |
| ---- | ---------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| 1    | `OFFSET_OUT_OF_RANGE`        | no        | Offset not in the log                                                                                                               |
| 3    | `UNKNOWN_TOPIC_OR_PARTITION` | yes       | Broker does not host this partition                                                                                                 |
| 5    | `LEADER_NOT_AVAILABLE`       | yes       | Leader election in progress                                                                                                         |
| 6    | `NOT_LEADER_OR_FOLLOWER`     | yes       | Wrong replica for this request                                                                                                      |
| 7    | `REQUEST_TIMED_OUT`          | yes       | Broker timed out                                                                                                                    |
| 10   | `MESSAGE_TOO_LARGE`          | no        | Broker rejected a request already sent - see `KafkaMessageTooLargeError` for the client-side `maxRequestSize` check that runs first |
| 16   | `NOT_ENOUGH_REPLICAS`        | yes       | ISR too small for acks                                                                                                              |
| 19   | `INVALID_TOPIC_EXCEPTION`    | no        | Illegal topic name                                                                                                                  |
| 27   | `REBALANCE_IN_PROGRESS`      | yes       | Group rebalancing                                                                                                                   |
| 29   | `TOPIC_AUTHORIZATION_FAILED` | no        | ACL                                                                                                                                 |
| 33   | `UNSUPPORTED_VERSION`        | no        | API version not supported                                                                                                           |

The file lists every protocol code the client knows. Prefer `error instanceof
KafkaProtocolError` and then `error.type`, not string matching on `message`.
