1#[derive(Clone, Copy, PartialEq, Eq, Hash)]
10pub struct ErrorCode(pub i16);
11
12macro_rules! error_codes {
13 ($($(#[$doc:meta])* $name:ident = $value:literal),+ $(,)?) => {
14 impl ErrorCode {
15 $($(#[$doc])* pub const $name: ErrorCode = ErrorCode($value);)+
16
17 pub const fn name(self) -> Option<&'static str> {
19 match self.0 {
20 $($value => Some(stringify!($name)),)+
21 _ => None,
22 }
23 }
24 }
25 };
26}
27
28error_codes! {
29 UNKNOWN_SERVER_ERROR = -1,
30 NONE = 0,
31 OFFSET_OUT_OF_RANGE = 1,
32 CORRUPT_MESSAGE = 2,
33 UNKNOWN_TOPIC_OR_PARTITION = 3,
34 INVALID_FETCH_SIZE = 4,
35 LEADER_NOT_AVAILABLE = 5,
36 NOT_LEADER_OR_FOLLOWER = 6,
37 REQUEST_TIMED_OUT = 7,
38 BROKER_NOT_AVAILABLE = 8,
39 REPLICA_NOT_AVAILABLE = 9,
40 MESSAGE_TOO_LARGE = 10,
41 NETWORK_EXCEPTION = 13,
42 COORDINATOR_LOAD_IN_PROGRESS = 14,
43 COORDINATOR_NOT_AVAILABLE = 15,
44 NOT_COORDINATOR = 16,
45 INVALID_TOPIC_EXCEPTION = 17,
46 RECORD_LIST_TOO_LARGE = 18,
47 NOT_ENOUGH_REPLICAS = 19,
48 NOT_ENOUGH_REPLICAS_AFTER_APPEND = 20,
49 INVALID_REQUIRED_ACKS = 21,
50 ILLEGAL_GENERATION = 22,
51 INCONSISTENT_GROUP_PROTOCOL = 23,
52 INVALID_GROUP_ID = 24,
53 UNKNOWN_MEMBER_ID = 25,
54 INVALID_SESSION_TIMEOUT = 26,
55 REBALANCE_IN_PROGRESS = 27,
56 TOPIC_AUTHORIZATION_FAILED = 29,
57 GROUP_AUTHORIZATION_FAILED = 30,
58 CLUSTER_AUTHORIZATION_FAILED = 31,
59 UNSUPPORTED_SASL_MECHANISM = 33,
60 ILLEGAL_SASL_STATE = 34,
61 UNSUPPORTED_VERSION = 35,
62 TOPIC_ALREADY_EXISTS = 36,
63 INVALID_PARTITIONS = 37,
64 INVALID_REPLICATION_FACTOR = 38,
65 NOT_CONTROLLER = 41,
66 INVALID_REQUEST = 42,
67 UNSUPPORTED_FOR_MESSAGE_FORMAT = 43,
68 POLICY_VIOLATION = 44,
69 OUT_OF_ORDER_SEQUENCE_NUMBER = 45,
70 DUPLICATE_SEQUENCE_NUMBER = 46,
71 INVALID_PRODUCER_EPOCH = 47,
72 INVALID_TXN_STATE = 48,
73 INVALID_PRODUCER_ID_MAPPING = 49,
74 INVALID_TRANSACTION_TIMEOUT = 50,
75 CONCURRENT_TRANSACTIONS = 51,
76 TRANSACTION_COORDINATOR_FENCED = 52,
77 TRANSACTIONAL_ID_AUTHORIZATION_FAILED = 53,
78 OPERATION_NOT_ATTEMPTED = 55,
79 KAFKA_STORAGE_ERROR = 56,
80 SASL_AUTHENTICATION_FAILED = 58,
81 UNKNOWN_PRODUCER_ID = 59,
82 TOPIC_DELETION_DISABLED = 73,
83 UNSUPPORTED_COMPRESSION_TYPE = 76,
84 MEMBER_ID_REQUIRED = 79,
85 GROUP_MAX_SIZE_REACHED = 81,
86 PRODUCER_FENCED = 90,
87 UNKNOWN_TOPIC_ID = 100,
88 TRANSACTIONAL_ID_NOT_FOUND = 105,
89 FENCED_MEMBER_EPOCH = 110,
90 UNRELEASED_INSTANCE_ID = 111,
91 UNSUPPORTED_ASSIGNOR = 112,
92 STALE_MEMBER_EPOCH = 113,
93 TRANSACTION_ABORTABLE = 120,
94}
95
96impl ErrorCode {
97 pub const fn is_ok(self) -> bool {
99 self.0 == 0
100 }
101}
102
103impl From<i16> for ErrorCode {
104 fn from(value: i16) -> Self {
105 ErrorCode(value)
106 }
107}
108
109impl std::fmt::Debug for ErrorCode {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 match self.name() {
112 Some(name) => write!(f, "ErrorCode({} {name})", self.0),
113 None => write!(f, "ErrorCode({})", self.0),
114 }
115 }
116}
117
118impl std::fmt::Display for ErrorCode {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 match self.name() {
121 Some(name) => write!(f, "{name} ({})", self.0),
122 None => write!(f, "unknown error code {}", self.0),
123 }
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[test]
132 fn names_and_display() {
133 assert_eq!(ErrorCode::NONE.name(), Some("NONE"));
134 assert!(ErrorCode::NONE.is_ok());
135 assert_eq!(ErrorCode::UNSUPPORTED_VERSION.0, 35);
136 assert_eq!(ErrorCode(9999).name(), None);
137 assert_eq!(
138 format!("{}", ErrorCode::UNSUPPORTED_VERSION),
139 "UNSUPPORTED_VERSION (35)"
140 );
141 }
142}