Skip to content

Commit b7b7f99

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

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ def get_by_id(self, schema_id):
168168
# cache it
169169
self._cache_schema(result, schema_id)
170170
return result
171-
except:
171+
except ClientError as e:
172172
# bad schema - should not happen
173-
raise ClientError("Received bad schema from registry.")
173+
raise ClientError("Received bad schema (id %s) from registry: %s" % (schema_id, e))
174174

175175
def get_latest_schema(self, subject):
176176
"""
@@ -204,9 +204,9 @@ def get_latest_schema(self, subject):
204204
else:
205205
try:
206206
schema = loads(result['schema'])
207-
except:
207+
except ClientError:
208208
# bad schema - should not happen
209-
raise ClientError("Received bad schema from registry.")
209+
raise
210210

211211
self._cache_schema(schema, schema_id, subject, version)
212212
return (schema_id, schema, version)
@@ -269,7 +269,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
269269
else:
270270
log.error("Unable to check the compatibility")
271271
False
272-
except:
272+
except Exception as e:
273+
log.error("_send_request() failed: %s", e)
273274
return False
274275

275276
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: 7 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,8 @@ 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 Exception:
183+
# Fast avro failed, fall thru to standard avro below.
184184
pass
185185

186186
# 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)