Skip to content

Commit d8b3eda

Browse files
committed
Fix flake8 warnings, mainly bare exceptions
1 parent 7ebc807 commit d8b3eda

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

confluent_kafka/avro/cached_schema_registry_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020
# derived from https://github.com/verisign/python-confluent-schemaregistry.git
2121
#
22+
import sys
2223
import json
2324
import logging
2425
from collections import defaultdict
@@ -168,9 +169,9 @@ def get_by_id(self, schema_id):
168169
# cache it
169170
self._cache_schema(result, schema_id)
170171
return result
171-
except:
172+
except ClientError as e:
172173
# bad schema - should not happen
173-
raise ClientError("Received bad schema from registry.")
174+
raise ClientError("Received bad schema (id %s) from registry: %s" % (schema_id, str(e)))
174175

175176
def get_latest_schema(self, subject):
176177
"""
@@ -204,9 +205,9 @@ def get_latest_schema(self, subject):
204205
else:
205206
try:
206207
schema = loads(result['schema'])
207-
except:
208+
except ClientError:
208209
# bad schema - should not happen
209-
raise ClientError("Received bad schema from registry.")
210+
raise
210211

211212
self._cache_schema(schema, schema_id, subject, version)
212213
return (schema_id, schema, version)
@@ -269,7 +270,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
269270
else:
270271
log.error("Unable to check the compatibility")
271272
False
272-
except:
273+
except: # noqa
274+
log.error("_send_request() failed: %s" % sys.exc_info()[0])
273275
return False
274276

275277
def update_compatibility(self, level, subject=None):

confluent_kafka/avro/load.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717

1818
import sys
1919

20+
from confluent_kafka.avro.error import ClientError
21+
2022

2123
def loads(schema_str):
2224
""" Parse a schema given a schema string """
23-
if sys.version_info[0] < 3:
24-
return schema.parse(schema_str)
25-
else:
26-
return schema.Parse(schema_str)
25+
try:
26+
if sys.version_info[0] < 3:
27+
return schema.parse(schema_str)
28+
else:
29+
return schema.Parse(schema_str)
30+
except schema.AvroException.SchemaParseException as e:
31+
raise ClientError("Schema parse failed: %s" % (str(e)))
2732

2833

2934
def load(fp):
@@ -44,5 +49,6 @@ def _hash_func(self):
4449
schema.RecordSchema.__hash__ = _hash_func
4550
schema.PrimitiveSchema.__hash__ = _hash_func
4651
schema.UnionSchema.__hash__ = _hash_func
52+
4753
except ImportError:
4854
schema = None

confluent_kafka/avro/serializer/message_serializer.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from fastavro.reader import read_data
4343

4444
HAS_FAST = True
45-
except:
45+
except ImportError:
4646
pass
4747

4848

@@ -158,12 +158,11 @@ def _get_decoder_func(self, schema_id, payload):
158158
# fetch from schema reg
159159
try:
160160
schema = self.registry_client.get_by_id(schema_id)
161-
except:
162-
schema = None
161+
except ClientError as e:
162+
raise SerializerError("unable to fetch schema with id %d: %s" % (schema_id, str(e)))
163163

164-
if not schema:
165-
err = "unable to fetch schema with id %d" % (schema_id)
166-
raise SerializerError(err)
164+
if schema is None:
165+
raise SerializerError("unable to fetch schema with id %d" % (schema_id))
167166

168167
curr_pos = payload.tell()
169168
if HAS_FAST:
@@ -180,7 +179,7 @@ def _get_decoder_func(self, schema_id, payload):
180179

181180
self.id_to_decoder_func[schema_id] = lambda p: read_data(p, schema_dict)
182181
return self.id_to_decoder_func[schema_id]
183-
except:
182+
except: # noqa
184183
pass
185184

186185
# here means we should just delegate to slow avro

examples/consumer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def print_usage_and_exit(program_name):
5959
continue
6060
try:
6161
intval = int(opt[1])
62-
except:
62+
except ValueError:
6363
sys.stderr.write("Invalid option value for -T: %s\n" % opt[1])
6464
sys.exit(1)
6565

tests/avro/mock_registry.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
from tests.avro.mock_schema_registry_client import MockSchemaRegistryClient
3030
from confluent_kafka import avro
31+
from confluent_kafka.avro.error import ClientError
32+
3133

3234
if sys.version_info[0] < 3:
3335
import BaseHTTPServer as HTTPSERVER
@@ -120,7 +122,7 @@ def _get_schema_from_body(self, req):
120122
try:
121123
avro_schema = avro.loads(schema)
122124
return self._get_identity_schema(avro_schema)
123-
except:
125+
except ClientError:
124126
return None
125127

126128
def register(self, req, groups):

tests/test_threads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
try:
77
from queue import Queue, Empty
8-
except:
8+
except ImportError:
99
from Queue import Queue, Empty
1010

1111

0 commit comments

Comments
 (0)