Skip to content

Fix bare except #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions confluent_kafka/avro/cached_schema_registry_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def get_by_id(self, schema_id):
# cache it
self._cache_schema(result, schema_id)
return result
except:
except ClientError as e:
# bad schema - should not happen
raise ClientError("Received bad schema from registry.")
raise ClientError("Received bad schema (id %s) from registry: %s" % (schema_id, e))

def get_latest_schema(self, subject):
"""
Expand Down Expand Up @@ -204,9 +204,9 @@ def get_latest_schema(self, subject):
else:
try:
schema = loads(result['schema'])
except:
except ClientError:
# bad schema - should not happen
raise ClientError("Received bad schema from registry.")
raise

self._cache_schema(schema, schema_id, subject, version)
return (schema_id, schema, version)
Expand Down Expand Up @@ -269,7 +269,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'):
else:
log.error("Unable to check the compatibility")
False
except:
except Exception as e:
log.error("_send_request() failed: %s", e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use log.exception()... it is the same as log.error() but it will automatically append the exception:

log.exception("_send_request() failed.")

return False

def update_compatibility(self, level, subject=None):
Expand Down
14 changes: 10 additions & 4 deletions confluent_kafka/avro/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

import sys

from confluent_kafka.avro.error import ClientError


def loads(schema_str):
""" Parse a schema given a schema string """
if sys.version_info[0] < 3:
return schema.parse(schema_str)
else:
return schema.Parse(schema_str)
try:
if sys.version_info[0] < 3:
return schema.parse(schema_str)
else:
return schema.Parse(schema_str)
except schema.AvroException.SchemaParseException as e:
raise ClientError("Schema parse failed: %s" % (str(e)))
Copy link
Contributor

@jeffwidman jeffwidman Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to cast to string...



def load(fp):
Expand All @@ -44,5 +49,6 @@ def _hash_func(self):
schema.RecordSchema.__hash__ = _hash_func
schema.PrimitiveSchema.__hash__ = _hash_func
schema.UnionSchema.__hash__ = _hash_func

except ImportError:
schema = None
14 changes: 7 additions & 7 deletions confluent_kafka/avro/serializer/message_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from fastavro.reader import read_data

HAS_FAST = True
except:
except ImportError:
pass


Expand Down Expand Up @@ -158,12 +158,11 @@ def _get_decoder_func(self, schema_id, payload):
# fetch from schema reg
try:
schema = self.registry_client.get_by_id(schema_id)
except:
schema = None
except ClientError as e:
raise SerializerError("unable to fetch schema with id %d: %s" % (schema_id, str(e)))
Copy link
Contributor

@jeffwidman jeffwidman Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to cast the exception to a string...


if not schema:
err = "unable to fetch schema with id %d" % (schema_id)
raise SerializerError(err)
if schema is None:
raise SerializerError("unable to fetch schema with id %d" % (schema_id))

curr_pos = payload.tell()
if HAS_FAST:
Expand All @@ -180,7 +179,8 @@ def _get_decoder_func(self, schema_id, payload):

self.id_to_decoder_func[schema_id] = lambda p: read_data(p, schema_dict)
return self.id_to_decoder_func[schema_id]
except:
except Exception:
# Fast avro failed, fall thru to standard avro below.
pass

# here means we should just delegate to slow avro
Expand Down
2 changes: 1 addition & 1 deletion examples/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def print_usage_and_exit(program_name):
continue
try:
intval = int(opt[1])
except:
except ValueError:
sys.stderr.write("Invalid option value for -T: %s\n" % opt[1])
sys.exit(1)

Expand Down
13 changes: 11 additions & 2 deletions tests/avro/mock_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import json
import re

from threading import Thread
from threading import Thread, Event

from tests.avro.mock_schema_registry_client import MockSchemaRegistryClient
from confluent_kafka import avro
from confluent_kafka.avro.error import ClientError


if sys.version_info[0] < 3:
import BaseHTTPServer as HTTPSERVER
Expand Down Expand Up @@ -120,7 +122,7 @@ def _get_schema_from_body(self, req):
try:
avro_schema = avro.loads(schema)
return self._get_identity_schema(avro_schema)
except:
except ClientError:
return None

def register(self, req, groups):
Expand Down Expand Up @@ -174,11 +176,18 @@ def __init__(self, port):
self.server = None
self.port = port
self.daemon = True
self.started = Event()

def run(self):
self.server = MockServer(('127.0.0.1', self.port), ReqHandler)
self.started.set()
self.server.serve_forever()

def start(self):
"""Start, and wait for server to be fully started, before returning."""
super(ServerThread, self).start()
self.started.wait()

def shutdown(self):
if self.server:
self.server.shutdown()
Expand Down
3 changes: 0 additions & 3 deletions tests/avro/test_cached_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
# derived from https://github.com/verisign/python-confluent-schemaregistry.git
#

import time

import unittest

from tests.avro import mock_registry
Expand All @@ -34,7 +32,6 @@ class TestCacheSchemaRegistryClient(unittest.TestCase):
def setUp(self):
self.server = mock_registry.ServerThread(0)
self.server.start()
time.sleep(1)
self.client = CachedSchemaRegistryClient('http://127.0.0.1:' + str(self.server.server.server_port))

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
try:
from queue import Queue, Empty
except:
except ImportError:
from Queue import Queue, Empty


Expand Down