Use reflection to avoid multiple errno definitions
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import inspect
|
||||||
|
import sys
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
###############
|
###############
|
||||||
@@ -79,9 +81,6 @@ class KafkaError(RuntimeError):
|
|||||||
class BrokerResponseError(KafkaError):
|
class BrokerResponseError(KafkaError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class NoError(BrokerResponseError):
|
|
||||||
errno = 0
|
|
||||||
message = 'SUCCESS'
|
|
||||||
|
|
||||||
class UnknownError(BrokerResponseError):
|
class UnknownError(BrokerResponseError):
|
||||||
errno = -1
|
errno = -1
|
||||||
@@ -201,27 +200,16 @@ class KafkaConfigurationError(KafkaError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
kafka_errors = {
|
def _iter_broker_errors():
|
||||||
-1 : UnknownError,
|
for name, obj in inspect.getmembers(sys.modules[__name__]):
|
||||||
0 : NoError,
|
if inspect.isclass(obj) and issubclass(obj, BrokerResponseError) and obj != BrokerResponseError:
|
||||||
1 : OffsetOutOfRangeError,
|
yield obj
|
||||||
2 : InvalidMessageError,
|
|
||||||
3 : UnknownTopicOrPartitionError,
|
|
||||||
4 : InvalidFetchRequestError,
|
kafka_errors = dict([(x.errno, x) for x in _iter_broker_errors()])
|
||||||
5 : LeaderNotAvailableError,
|
|
||||||
6 : NotLeaderForPartitionError,
|
|
||||||
7 : RequestTimedOutError,
|
|
||||||
8 : BrokerNotAvailableError,
|
|
||||||
9 : ReplicaNotAvailableError,
|
|
||||||
10 : MessageSizeTooLargeError,
|
|
||||||
11 : StaleControllerEpochError,
|
|
||||||
12 : OffsetMetadataTooLargeError,
|
|
||||||
13 : StaleLeaderEpochCodeError,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def check_error(response):
|
def check_error(response):
|
||||||
error = kafka_errors.get(response.error, UnknownError)
|
if response.error:
|
||||||
if error is not NoError:
|
error_class = kafka_errors.get(response.error, UnknownError)
|
||||||
raise error(response)
|
raise error_class(response)
|
||||||
|
|
||||||
|
@@ -10,9 +10,8 @@ from kafka.common import (
|
|||||||
ProduceRequest, MetadataResponse,
|
ProduceRequest, MetadataResponse,
|
||||||
BrokerMetadata, TopicMetadata, PartitionMetadata,
|
BrokerMetadata, TopicMetadata, PartitionMetadata,
|
||||||
TopicAndPartition, KafkaUnavailableError,
|
TopicAndPartition, KafkaUnavailableError,
|
||||||
LeaderNotAvailableError, NoError,
|
LeaderNotAvailableError, UnknownTopicOrPartitionError,
|
||||||
UnknownTopicOrPartitionError, KafkaTimeoutError,
|
KafkaTimeoutError, ConnectionError
|
||||||
ConnectionError
|
|
||||||
)
|
)
|
||||||
from kafka.conn import KafkaConnection
|
from kafka.conn import KafkaConnection
|
||||||
from kafka.protocol import KafkaProtocol, create_message
|
from kafka.protocol import KafkaProtocol, create_message
|
||||||
|
Reference in New Issue
Block a user