1515# See the License for the specific language governing permissions and
1616# limitations under the License.
1717#
18+ from confluent_kafka .cimpl import KafkaException , KafkaError
1819
20+ from confluent_kafka .serialization import SerializationError
1921
20- class ConsumeError (Exception ):
21- __slots__ = ['message' , 'reason' , 'error_code' ]
2222
23+ class ConsumeError (KafkaException ):
2324 """
2425 Wraps all errors encountered during the consumption of a message.
2526
@@ -28,25 +29,113 @@ class ConsumeError(Exception):
2829 may be retrieved from the ``message`` attribute.
2930
3031 Args:
31- error (KafkaError): The error that occurred .
32+ error_code (KafkaError): Error code indicating the type of error .
3233
33- message (Message , optional): The message returned from the broker.
34+ exception(Exception , optional): The original exception
3435
35- reason (str ): String description of the error .
36+ message (Message, optional ): The Kafka Message returned from the broker .
3637
3738 """
39+ def __init__ (self , error_code , exception = None , message = None ):
40+ if exception is not None :
41+ kafka_error = KafkaError (error_code , repr (exception ))
42+ self .exception = exception
43+ else :
44+ kafka_error = KafkaError (error_code )
45+ self .exception = None
3846
39- def __init__ (self , error , reason = None , message = None ):
40- self .error = error
41- if reason is None :
42- reason = error .str ()
43-
44- self .reason = reason
47+ super (ConsumeError , self ).__init__ (kafka_error )
4548 self .message = message
4649
47- def __repr__ (self ):
48- return str (self )
50+ @property
51+ def code (self ):
52+ return self .code ()
53+
54+ @property
55+ def name (self ):
56+ return self .name ()
57+
58+
59+ class KeyDeserializationError (ConsumeError , SerializationError ):
60+ """
61+ Wraps all errors encountered during the deserialization of a Kafka
62+ Message's key.
63+
64+ Args:
65+ exception(Exception, optional): The original exception
66+
67+ message (Message, optional): The Kafka Message returned from the broker.
68+
69+ """
70+ def __init__ (self , exception = None , message = None ):
71+ super (KeyDeserializationError , self ).__init__ (
72+ KafkaError ._KEY_DESERIALIZATION , exception = exception , message = message )
73+
74+
75+ class ValueDeserializationError (ConsumeError , SerializationError ):
76+ """
77+ Wraps all errors encountered during the deserialization of a Kafka
78+ Message's value.
79+
80+ Args:
81+ exception(Exception, optional): The original exception
82+
83+ message (Message, optional): The Kafka Message returned from the broker.
84+
85+ """
86+ def __init__ (self , exception = None , message = None ):
87+ super (ValueDeserializationError , self ).__init__ (
88+ KafkaError ._VALUE_DESERIALIZATION , exception = exception , message = message )
89+
90+
91+ class ProduceError (KafkaException ):
92+ """
93+ Wraps all errors encountered when Producing messages.
94+
95+ Args:
96+ error_code (KafkaError): Error code indicating the type of error.
97+
98+ exception(Exception, optional): The original exception.
99+
100+ """
101+ def __init__ (self , error_code , exception = None ):
102+ if exception is not None :
103+ kafka_error = KafkaError (error_code , repr (exception ))
104+ self .exception = exception
105+ else :
106+ kafka_error = KafkaError (error_code )
107+ self .exception = None
108+
109+ super (ProduceError , self ).__init__ (kafka_error )
110+
111+ @property
112+ def code (self ):
113+ return self .code ()
114+
115+ @property
116+ def name (self ):
117+ return self .name ()
118+
49119
50- def __str__ (self ):
51- return "{} (KafkaError code {})" .format (self .reason ,
52- self .error )
120+ class KeySerializationError (ProduceError , SerializationError ):
121+ """
122+ Wraps all errors encountered during the serialization of a Message key.
123+
124+ Args:
125+ exception (Exception): The exception that occurred during serialization.
126+ """
127+ def __init__ (self , exception = None ):
128+ super (KeySerializationError , self ).__init__ (
129+ KafkaError ._KEY_SERIALIZATION , exception = exception )
130+
131+
132+ class ValueSerializationError (ProduceError , SerializationError ):
133+ """
134+ Wraps all errors encountered during the serialization of a Message value.
135+
136+ Args:
137+ exception (Exception): The exception that occurred during serialization.
138+ """
139+ def __init__ (self , exception = None ):
140+ super (ValueSerializationError , self ).__init__ (
141+ KafkaError ._VALUE_SERIALIZATION , exception = exception )
0 commit comments