Be more tolerant of unicode exceptions

When an exception message is in unicode we should not fail
when trying to get that unicode containing exceptions message
so instead of using str() use a new utils function that has
been working in other projects to attempt to convert a exception
message into its corresponding unicode in a safer manner.

Change-Id: I202f12d8f9bf562972b760f91805dba2ad14d804
This commit is contained in:
Joshua Harlow 2014-09-16 17:32:22 -07:00 committed by Julien Danjou
parent a07fe43b3d
commit 9d0836c710
3 changed files with 35 additions and 8 deletions

View File

@ -26,6 +26,7 @@ import six
from tooz import coordination
from tooz import locking
from tooz import utils
LOG = logging.getLogger(__name__)
@ -131,7 +132,7 @@ class MemcachedDriver(coordination.CoordinationDriver):
timeout=self.timeout,
connect_timeout=self.timeout)
except Exception as e:
raise coordination.ToozConnectionError(e)
raise coordination.ToozConnectionError(utils.exception_message(e))
self._group_members = collections.defaultdict(set)
self._acquired_locks = []
self.heartbeat()

View File

@ -24,6 +24,7 @@ import six
from tooz import coordination
from tooz import locking
from tooz import utils
class ZooKeeperLock(locking.Lock):
@ -87,7 +88,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.ToozError("tooz namespace has not been created")
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
def create_group(self, group_id):
group_path = self._path_group(group_id)
@ -104,7 +105,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.GroupNotCreated(group_id)
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
def join_group(self, group_id, capabilities=b""):
member_path = self._path_member(group_id, self._member_id)
@ -121,7 +122,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
def leave_group(self, group_id):
member_path = self._path_member(group_id, self._member_id)
@ -136,7 +137,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.GroupNotCreated(group_id)
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
else:
return set(m.encode('ascii') for m in members_ids)
@ -154,7 +155,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
def update_capabilities(self, group_id, capabilities):
member_path = self._path_member(group_id, self._member_id)
@ -170,7 +171,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
else:
return capabilities
@ -188,7 +189,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
except exceptions.NoNodeError:
raise coordination.ToozError("tooz namespace has not been created")
except exceptions.ZookeeperError as e:
raise coordination.ToozError(str(e))
raise coordination.ToozError(utils.exception_message(e))
else:
return set(g.encode('ascii') for g in group_ids)

25
tooz/utils.py Normal file
View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import six
def exception_message(exc):
"""Return the string representation of exception."""
try:
return six.text_type(exc)
except UnicodeError:
return str(exc)