Skip to content

Commit b1bf1b0

Browse files
committed
Split SerializerError into KeySerializerError and ValueSerializerError
1 parent 0d4ba49 commit b1bf1b0

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

confluent_kafka/avro/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def __str__(self):
5353

5454

5555
from confluent_kafka.avro.cached_schema_registry_client import CachedSchemaRegistryClient
56-
from confluent_kafka.avro.serializer import SerializerError
56+
from confluent_kafka.avro.serializer import (SerializerError,
57+
KeySerializerError,
58+
ValueSerializerError)
5759
from confluent_kafka.avro.serializer.message_serializer import MessageSerializer
5860

5961

@@ -99,17 +101,19 @@ def produce(self, **kwargs):
99101
raise ClientError("Topic name not specified.")
100102
value = kwargs.pop('value', None)
101103
key = kwargs.pop('key', None)
104+
102105
if value:
103106
if value_schema:
104107
value = self._serializer.encode_record_with_schema(topic, value_schema, value)
105108
else:
106-
raise SerializerError("Avro schema required for value")
109+
raise ValueSerializerError("Avro schema required for values")
107110

108111
if key:
109112
if key_schema:
110113
key = self._serializer.encode_record_with_schema(topic, key_schema, key, True)
111114
else:
112-
raise SerializerError("Avro schema required for key")
115+
raise KeySerializerError("Avro schema required for key")
116+
113117

114118
super(AvroProducer, self).produce(topic, value, key, **kwargs)
115119

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2016 Confluent Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
119
class SerializerError(Exception):
220
"""Generic error from serializer package"""
321

422
def __init__(self, message):
523
self.message = message
624

725
def __repr__(self):
8-
return 'SerializerError(error={error})'.format(error=self.message)
26+
return '{klass}(error={error})'.format(
27+
klass=self.__class__.__name__,
28+
error=self.message
29+
)
930

1031
def __str__(self):
1132
return self.message
33+
34+
35+
class KeySerializerError(SerializerError):
36+
pass
37+
38+
39+
class ValueSerializerError(SerializerError):
40+
pass

confluent_kafka/avro/serializer/message_serializer.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import avro.io
3030

3131
from confluent_kafka.avro import ClientError
32-
from . import SerializerError
32+
from confluent_kafka.avro.serializer import (SerializerError,
33+
KeySerializerError,
34+
ValueSerializerError)
3335

3436
log = logging.getLogger(__name__)
3537

@@ -84,46 +86,52 @@ def encode_record_with_schema(self, topic, schema, record, is_key=False):
8486
@:param topic : Topic name
8587
@:param schema : Avro Schema
8688
@:param record : A dictionary object
89+
@:param is_key : If the record is a key
8790
@:returns : Encoded record with schema ID as bytes
8891
"""
8992
if not isinstance(record, dict):
9093
raise SerializerError("record must be a dictionary")
94+
serialize_err = KeySerializerError if is_key else ValueSerializerError
95+
9196
subject_suffix = ('-key' if is_key else '-value')
9297
# get the latest schema for the subject
9398
subject = topic + subject_suffix
9499
# register it
95100
schema_id = self.registry_client.register(subject, schema)
96101
if not schema_id:
97102
message = "Unable to retrieve schema id for subject %s" % (subject)
98-
raise SerializerError(message)
103+
raise serialize_err(message)
99104

100105
# cache writer
101106
self.id_to_writers[schema_id] = avro.io.DatumWriter(schema)
102107

103-
return self.encode_record_with_schema_id(schema_id, record)
108+
return self.encode_record_with_schema_id(schema_id, record, is_key=is_key)
104109

105-
def encode_record_with_schema_id(self, schema_id, record):
110+
def encode_record_with_schema_id(self, schema_id, record, is_key=False):
106111
"""
107112
Encode a record with a given schema id. The record must
108113
be a python dictionary.
109114
@:param: schema_id : integer ID
110115
@:param: record : A dictionary object
116+
@:param is_key : If the record is a key
111117
@:returns: decoder function
112118
"""
113119
if not isinstance(record, dict):
114120
raise SerializerError("record must be a dictionary")
121+
serialize_err = KeySerializerError if is_key else ValueSerializerError
122+
115123
# use slow avro
116124
if schema_id not in self.id_to_writers:
117125
# get the writer + schema
118126

119127
try:
120128
schema = self.registry_client.get_by_id(schema_id)
121129
if not schema:
122-
raise SerializerError("Schema does not exist")
130+
raise serialize_err("Schema does not exist")
123131
self.id_to_writers[schema_id] = avro.io.DatumWriter(schema)
124132
except ClientError as e:
125133
exc_type, exc_value, exc_traceback = sys.exc_info()
126-
raise SerializerError("Error fetching schema from registry:" + repr(
134+
raise serialize_err( + repr(
127135
traceback.format_exception(exc_type, exc_value, exc_traceback)))
128136

129137
# get the writer

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy