'How to handle exceptions in Confluent-Kafka for python

I'm trying to use confluent_kafka to consume some messages from a broker. I'm getting an exception which I fully understand. I'm trying to get earliest offsets for a topic/partition which doesn't exist.

I'm trying to handle this exception properly and I see that when I print out the exception I get

cimpl.KafkaException: KafkaError{code=_UNKNOWN_PARTITION,val=-190,str="Failed to get offsets: Local: Unknown partition"}

I'd like to put a nice try/except block around this and handle the UNKNOWN_PARTITION error code but I can't find any constants that match the _UNKNOWN_PARTITION value (-190). I don't want to hard code the -190 into my code so does anyone know how I should do this?

try:
    earliest_offsets = self._kafka.offsets_for_times( topic_partitions )
except KafkaException as exception:
    kafka_error = exception.args[ 0 ]
    if kafka_error.code() == <SOME-CONSTANT-GOES-HERE>:
        print( "Kafka Topic/Partition Does Not Exist!!" )


Solution 1:[1]

Ah. It turns out the constants I require are on the KafkaError class itself, i.e.

try:
    earliest_offsets = self._kafka.offsets_for_times( topic_partitions )
except KafkaException as exception:
    kafka_error = exception.args[ 0 ]
    if kafka_error.code() == KafkaError._UNKNOWN_PARTITION:
        print( "Kafka Topic/Partition Does Not Exist!!" )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ScaryAardvark