rskafka/protocol/
error.rs

1//! Error codes.
2//!
3//! # References
4//! - <https://kafka.apache.org/protocol#protocol_error_codes>
5
6use super::primitives::Int16;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[allow(clippy::enum_variant_names)]
10#[non_exhaustive]
11pub enum Error {
12    UnknownServerError,
13    OffsetOutOfRange,
14    CorruptMessage,
15    UnknownTopicOrPartition,
16    InvalidFetchSize,
17    LeaderNotAvailable,
18    NotLeaderOrFollower,
19    RequestTimedOut,
20    BrokerNotAvailable,
21    ReplicaNotAvailable,
22    MessageTooLarge,
23    StaleControllerEpoch,
24    OffsetMetadataTooLarge,
25    NetworkException,
26    CoordinatorLoadInProgress,
27    CoordinatorNotAvailable,
28    NotCoordinator,
29    InvalidTopicException,
30    RecordListTooLarge,
31    NotEnoughReplicas,
32    NotEnoughReplicasAfterAppend,
33    InvalidRequiredAcks,
34    IllegalGeneration,
35    InconsistentGroupProtocol,
36    InvalidGroupId,
37    UnknownMemberId,
38    InvalidSessionTimeout,
39    RebalanceInProgress,
40    InvalidCommitOffsetSize,
41    TopicAuthorizationFailed,
42    GroupAuthorizationFailed,
43    ClusterAuthorizationFailed,
44    InvalidTimestamp,
45    UnsupportedSaslMechanism,
46    IllegalSaslState,
47    UnsupportedVersion,
48    TopicAlreadyExists,
49    InvalidPartitions,
50    InvalidReplicationFactor,
51    InvalidReplicaAssignment,
52    InvalidConfig,
53    NotController,
54    InvalidRequest,
55    UnsupportedForMessageFormat,
56    PolicyViolation,
57    OutOfOrderSequenceNumber,
58    DuplicateSequenceNumber,
59    InvalidProducerEpoch,
60    InvalidTxnState,
61    InvalidProducerIdMapping,
62    InvalidTransactionTimeout,
63    ConcurrentTransactions,
64    TransactionCoordinatorFenced,
65    TransactionalIdAuthorizationFailed,
66    SecurityDisabled,
67    OperationNotAttempted,
68    KafkaStorageError,
69    LogDirNotFound,
70    SaslAuthenticationFailed,
71    UnknownProducerId,
72    ReassignmentInProgress,
73    DelegationTokenAuthDisabled,
74    DelegationTokenNotFound,
75    DelegationTokenOwnerMismatch,
76    DelegationTokenRequestNotAllowed,
77    DelegationTokenAuthorizationFailed,
78    DelegationTokenExpired,
79    InvalidPrincipalType,
80    NonEmptyGroup,
81    GroupIdNotFound,
82    FetchSessionIdNotFound,
83    InvalidFetchSessionEpoch,
84    ListenerNotFound,
85    TopicDeletionDisabled,
86    FencedLeaderEpoch,
87    UnknownLeaderEpoch,
88    UnsupportedCompressionType,
89    StaleBrokerEpoch,
90    OffsetNotAvailable,
91    MemberIdRequired,
92    PreferredLeaderNotAvailable,
93    GroupMaxSizeReached,
94    FencedInstanceId,
95    EligibleLeadersNotAvailable,
96    ElectionNotNeeded,
97    NoReassignmentInProgress,
98    GroupSubscribedToTopic,
99    InvalidRecord,
100    UnstableOffsetCommit,
101    ThrottlingQuotaExceeded,
102    ProducerFenced,
103    ResourceNotFound,
104    DuplicateResource,
105    UnacceptableCredential,
106    InconsistentVoterSet,
107    InvalidUpdateVersion,
108    FeatureUpdateFailed,
109    PrincipalDeserializationFailure,
110    SnapshotNotFound,
111    PositionOutOfRange,
112    UnknownTopicId,
113    DuplicateBrokerRegistration,
114    BrokerIdNotRegistered,
115    InconsistentTopicId,
116    InconsistentClusterId,
117    TransactionalIdNotFound,
118    Unknown(i16),
119}
120
121impl Error {
122    /// Coversion from [`i16`].
123    ///
124    /// This cannot be a `From` trait due to <https://github.com/rust-lang/rfcs/issues/1402>.
125    pub fn new(code: i16) -> Option<Self> {
126        match code {
127            -1 => Some(Self::UnknownServerError),
128            0 => None,
129            1 => Some(Self::OffsetOutOfRange),
130            2 => Some(Self::CorruptMessage),
131            3 => Some(Self::UnknownTopicOrPartition),
132            4 => Some(Self::InvalidFetchSize),
133            5 => Some(Self::LeaderNotAvailable),
134            6 => Some(Self::NotLeaderOrFollower),
135            7 => Some(Self::RequestTimedOut),
136            8 => Some(Self::BrokerNotAvailable),
137            9 => Some(Self::ReplicaNotAvailable),
138            10 => Some(Self::MessageTooLarge),
139            11 => Some(Self::StaleControllerEpoch),
140            12 => Some(Self::OffsetMetadataTooLarge),
141            13 => Some(Self::NetworkException),
142            14 => Some(Self::CoordinatorLoadInProgress),
143            15 => Some(Self::CoordinatorNotAvailable),
144            16 => Some(Self::NotCoordinator),
145            17 => Some(Self::InvalidTopicException),
146            18 => Some(Self::RecordListTooLarge),
147            19 => Some(Self::NotEnoughReplicas),
148            20 => Some(Self::NotEnoughReplicasAfterAppend),
149            21 => Some(Self::InvalidRequiredAcks),
150            22 => Some(Self::IllegalGeneration),
151            23 => Some(Self::InconsistentGroupProtocol),
152            24 => Some(Self::InvalidGroupId),
153            25 => Some(Self::UnknownMemberId),
154            26 => Some(Self::InvalidSessionTimeout),
155            27 => Some(Self::RebalanceInProgress),
156            28 => Some(Self::InvalidCommitOffsetSize),
157            29 => Some(Self::TopicAuthorizationFailed),
158            30 => Some(Self::GroupAuthorizationFailed),
159            31 => Some(Self::ClusterAuthorizationFailed),
160            32 => Some(Self::InvalidTimestamp),
161            33 => Some(Self::UnsupportedSaslMechanism),
162            34 => Some(Self::IllegalSaslState),
163            35 => Some(Self::UnsupportedVersion),
164            36 => Some(Self::TopicAlreadyExists),
165            37 => Some(Self::InvalidPartitions),
166            38 => Some(Self::InvalidReplicationFactor),
167            39 => Some(Self::InvalidReplicaAssignment),
168            40 => Some(Self::InvalidConfig),
169            41 => Some(Self::NotController),
170            42 => Some(Self::InvalidRequest),
171            43 => Some(Self::UnsupportedForMessageFormat),
172            44 => Some(Self::PolicyViolation),
173            45 => Some(Self::OutOfOrderSequenceNumber),
174            46 => Some(Self::DuplicateSequenceNumber),
175            47 => Some(Self::InvalidProducerEpoch),
176            48 => Some(Self::InvalidTxnState),
177            49 => Some(Self::InvalidProducerIdMapping),
178            50 => Some(Self::InvalidTransactionTimeout),
179            51 => Some(Self::ConcurrentTransactions),
180            52 => Some(Self::TransactionCoordinatorFenced),
181            53 => Some(Self::TransactionalIdAuthorizationFailed),
182            54 => Some(Self::SecurityDisabled),
183            55 => Some(Self::OperationNotAttempted),
184            56 => Some(Self::KafkaStorageError),
185            57 => Some(Self::LogDirNotFound),
186            58 => Some(Self::SaslAuthenticationFailed),
187            59 => Some(Self::UnknownProducerId),
188            60 => Some(Self::ReassignmentInProgress),
189            61 => Some(Self::DelegationTokenAuthDisabled),
190            62 => Some(Self::DelegationTokenNotFound),
191            63 => Some(Self::DelegationTokenOwnerMismatch),
192            64 => Some(Self::DelegationTokenRequestNotAllowed),
193            65 => Some(Self::DelegationTokenAuthorizationFailed),
194            66 => Some(Self::DelegationTokenExpired),
195            67 => Some(Self::InvalidPrincipalType),
196            68 => Some(Self::NonEmptyGroup),
197            69 => Some(Self::GroupIdNotFound),
198            70 => Some(Self::FetchSessionIdNotFound),
199            71 => Some(Self::InvalidFetchSessionEpoch),
200            72 => Some(Self::ListenerNotFound),
201            73 => Some(Self::TopicDeletionDisabled),
202            74 => Some(Self::FencedLeaderEpoch),
203            75 => Some(Self::UnknownLeaderEpoch),
204            76 => Some(Self::UnsupportedCompressionType),
205            77 => Some(Self::StaleBrokerEpoch),
206            78 => Some(Self::OffsetNotAvailable),
207            79 => Some(Self::MemberIdRequired),
208            80 => Some(Self::PreferredLeaderNotAvailable),
209            81 => Some(Self::GroupMaxSizeReached),
210            82 => Some(Self::FencedInstanceId),
211            83 => Some(Self::EligibleLeadersNotAvailable),
212            84 => Some(Self::ElectionNotNeeded),
213            85 => Some(Self::NoReassignmentInProgress),
214            86 => Some(Self::GroupSubscribedToTopic),
215            87 => Some(Self::InvalidRecord),
216            88 => Some(Self::UnstableOffsetCommit),
217            89 => Some(Self::ThrottlingQuotaExceeded),
218            90 => Some(Self::ProducerFenced),
219            91 => Some(Self::ResourceNotFound),
220            92 => Some(Self::DuplicateResource),
221            93 => Some(Self::UnacceptableCredential),
222            94 => Some(Self::InconsistentVoterSet),
223            95 => Some(Self::InvalidUpdateVersion),
224            96 => Some(Self::FeatureUpdateFailed),
225            97 => Some(Self::PrincipalDeserializationFailure),
226            98 => Some(Self::SnapshotNotFound),
227            99 => Some(Self::PositionOutOfRange),
228            100 => Some(Self::UnknownTopicId),
229            101 => Some(Self::DuplicateBrokerRegistration),
230            102 => Some(Self::BrokerIdNotRegistered),
231            103 => Some(Self::InconsistentTopicId),
232            104 => Some(Self::InconsistentClusterId),
233            105 => Some(Self::TransactionalIdNotFound),
234            _ => Some(Self::Unknown(code)),
235        }
236    }
237}
238
239impl From<Option<Error>> for Int16 {
240    fn from(error: Option<Error>) -> Self {
241        let error = match error {
242            Some(error) => error,
243            None => return Self(0),
244        };
245
246        match error {
247            Error::UnknownServerError => Self(-1),
248            Error::OffsetOutOfRange => Self(1),
249            Error::CorruptMessage => Self(2),
250            Error::UnknownTopicOrPartition => Self(3),
251            Error::InvalidFetchSize => Self(4),
252            Error::LeaderNotAvailable => Self(5),
253            Error::NotLeaderOrFollower => Self(6),
254            Error::RequestTimedOut => Self(7),
255            Error::BrokerNotAvailable => Self(8),
256            Error::ReplicaNotAvailable => Self(9),
257            Error::MessageTooLarge => Self(10),
258            Error::StaleControllerEpoch => Self(11),
259            Error::OffsetMetadataTooLarge => Self(12),
260            Error::NetworkException => Self(13),
261            Error::CoordinatorLoadInProgress => Self(14),
262            Error::CoordinatorNotAvailable => Self(15),
263            Error::NotCoordinator => Self(16),
264            Error::InvalidTopicException => Self(17),
265            Error::RecordListTooLarge => Self(18),
266            Error::NotEnoughReplicas => Self(19),
267            Error::NotEnoughReplicasAfterAppend => Self(20),
268            Error::InvalidRequiredAcks => Self(21),
269            Error::IllegalGeneration => Self(22),
270            Error::InconsistentGroupProtocol => Self(23),
271            Error::InvalidGroupId => Self(24),
272            Error::UnknownMemberId => Self(25),
273            Error::InvalidSessionTimeout => Self(26),
274            Error::RebalanceInProgress => Self(27),
275            Error::InvalidCommitOffsetSize => Self(28),
276            Error::TopicAuthorizationFailed => Self(29),
277            Error::GroupAuthorizationFailed => Self(30),
278            Error::ClusterAuthorizationFailed => Self(31),
279            Error::InvalidTimestamp => Self(32),
280            Error::UnsupportedSaslMechanism => Self(33),
281            Error::IllegalSaslState => Self(34),
282            Error::UnsupportedVersion => Self(35),
283            Error::TopicAlreadyExists => Self(36),
284            Error::InvalidPartitions => Self(37),
285            Error::InvalidReplicationFactor => Self(38),
286            Error::InvalidReplicaAssignment => Self(39),
287            Error::InvalidConfig => Self(40),
288            Error::NotController => Self(41),
289            Error::InvalidRequest => Self(42),
290            Error::UnsupportedForMessageFormat => Self(43),
291            Error::PolicyViolation => Self(44),
292            Error::OutOfOrderSequenceNumber => Self(45),
293            Error::DuplicateSequenceNumber => Self(46),
294            Error::InvalidProducerEpoch => Self(47),
295            Error::InvalidTxnState => Self(48),
296            Error::InvalidProducerIdMapping => Self(49),
297            Error::InvalidTransactionTimeout => Self(50),
298            Error::ConcurrentTransactions => Self(51),
299            Error::TransactionCoordinatorFenced => Self(52),
300            Error::TransactionalIdAuthorizationFailed => Self(53),
301            Error::SecurityDisabled => Self(54),
302            Error::OperationNotAttempted => Self(55),
303            Error::KafkaStorageError => Self(56),
304            Error::LogDirNotFound => Self(57),
305            Error::SaslAuthenticationFailed => Self(58),
306            Error::UnknownProducerId => Self(59),
307            Error::ReassignmentInProgress => Self(60),
308            Error::DelegationTokenAuthDisabled => Self(61),
309            Error::DelegationTokenNotFound => Self(62),
310            Error::DelegationTokenOwnerMismatch => Self(63),
311            Error::DelegationTokenRequestNotAllowed => Self(64),
312            Error::DelegationTokenAuthorizationFailed => Self(65),
313            Error::DelegationTokenExpired => Self(66),
314            Error::InvalidPrincipalType => Self(67),
315            Error::NonEmptyGroup => Self(68),
316            Error::GroupIdNotFound => Self(69),
317            Error::FetchSessionIdNotFound => Self(70),
318            Error::InvalidFetchSessionEpoch => Self(71),
319            Error::ListenerNotFound => Self(72),
320            Error::TopicDeletionDisabled => Self(73),
321            Error::FencedLeaderEpoch => Self(74),
322            Error::UnknownLeaderEpoch => Self(75),
323            Error::UnsupportedCompressionType => Self(76),
324            Error::StaleBrokerEpoch => Self(77),
325            Error::OffsetNotAvailable => Self(78),
326            Error::MemberIdRequired => Self(79),
327            Error::PreferredLeaderNotAvailable => Self(80),
328            Error::GroupMaxSizeReached => Self(81),
329            Error::FencedInstanceId => Self(82),
330            Error::EligibleLeadersNotAvailable => Self(83),
331            Error::ElectionNotNeeded => Self(84),
332            Error::NoReassignmentInProgress => Self(85),
333            Error::GroupSubscribedToTopic => Self(86),
334            Error::InvalidRecord => Self(87),
335            Error::UnstableOffsetCommit => Self(88),
336            Error::ThrottlingQuotaExceeded => Self(89),
337            Error::ProducerFenced => Self(90),
338            Error::ResourceNotFound => Self(91),
339            Error::DuplicateResource => Self(92),
340            Error::UnacceptableCredential => Self(93),
341            Error::InconsistentVoterSet => Self(94),
342            Error::InvalidUpdateVersion => Self(95),
343            Error::FeatureUpdateFailed => Self(96),
344            Error::PrincipalDeserializationFailure => Self(97),
345            Error::SnapshotNotFound => Self(98),
346            Error::PositionOutOfRange => Self(99),
347            Error::UnknownTopicId => Self(100),
348            Error::DuplicateBrokerRegistration => Self(101),
349            Error::BrokerIdNotRegistered => Self(102),
350            Error::InconsistentTopicId => Self(103),
351            Error::InconsistentClusterId => Self(104),
352            Error::TransactionalIdNotFound => Self(105),
353            Error::Unknown(code) => Self(code),
354        }
355    }
356}
357
358impl std::fmt::Display for Error {
359    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
360        write!(f, "{:?}", self)
361    }
362}
363
364impl std::error::Error for Error {}