|
| 1 | +import inspect |
| 2 | +import sys |
1 | 3 | from collections import namedtuple
|
2 | 4 |
|
3 | 5 | ###############
|
@@ -79,9 +81,6 @@ class KafkaError(RuntimeError):
|
79 | 81 | class BrokerResponseError(KafkaError):
|
80 | 82 | pass
|
81 | 83 |
|
82 |
| -class NoError(BrokerResponseError): |
83 |
| - errno = 0 |
84 |
| - message = 'SUCCESS' |
85 | 84 |
|
86 | 85 | class UnknownError(BrokerResponseError):
|
87 | 86 | errno = -1
|
@@ -201,27 +200,16 @@ class KafkaConfigurationError(KafkaError):
|
201 | 200 | pass
|
202 | 201 |
|
203 | 202 |
|
204 |
| -kafka_errors = { |
205 |
| - -1 : UnknownError, |
206 |
| - 0 : NoError, |
207 |
| - 1 : OffsetOutOfRangeError, |
208 |
| - 2 : InvalidMessageError, |
209 |
| - 3 : UnknownTopicOrPartitionError, |
210 |
| - 4 : InvalidFetchRequestError, |
211 |
| - 5 : LeaderNotAvailableError, |
212 |
| - 6 : NotLeaderForPartitionError, |
213 |
| - 7 : RequestTimedOutError, |
214 |
| - 8 : BrokerNotAvailableError, |
215 |
| - 9 : ReplicaNotAvailableError, |
216 |
| - 10 : MessageSizeTooLargeError, |
217 |
| - 11 : StaleControllerEpochError, |
218 |
| - 12 : OffsetMetadataTooLargeError, |
219 |
| - 13 : StaleLeaderEpochCodeError, |
220 |
| -} |
| 203 | +def _iter_broker_errors(): |
| 204 | + for name, obj in inspect.getmembers(sys.modules[__name__]): |
| 205 | + if inspect.isclass(obj) and issubclass(obj, BrokerResponseError) and obj != BrokerResponseError: |
| 206 | + yield obj |
221 | 207 |
|
222 | 208 |
|
223 |
| -def check_error(response): |
224 |
| - error = kafka_errors.get(response.error, UnknownError) |
225 |
| - if error is not NoError: |
226 |
| - raise error(response) |
| 209 | +kafka_errors = dict([(x.errno, x) for x in _iter_broker_errors()]) |
| 210 | + |
227 | 211 |
|
| 212 | +def check_error(response): |
| 213 | + if response.error: |
| 214 | + error_class = kafka_errors.get(response.error, UnknownError) |
| 215 | + raise error_class(response) |
0 commit comments