summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2014-09-16 17:32:22 -0700
committerJulien Danjou <julien@danjou.info>2014-09-18 17:08:11 +0200
commit9d0836c710f1d7fc248c014f2726c686046a2ee3 (patch)
tree16baa24ef9028f4c96c134106eac41217a3fcdfe
parenta07fe43b3d608de6df102d37a3b1fab0b0f93fa0 (diff)
downloadtooz-9d0836c710f1d7fc248c014f2726c686046a2ee3.tar.gz
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
-rw-r--r--tooz/drivers/memcached.py3
-rw-r--r--tooz/drivers/zookeeper.py15
-rw-r--r--tooz/utils.py25
3 files changed, 35 insertions, 8 deletions
diff --git a/tooz/drivers/memcached.py b/tooz/drivers/memcached.py
index 999b711..54ad27e 100644
--- a/tooz/drivers/memcached.py
+++ b/tooz/drivers/memcached.py
@@ -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()
diff --git a/tooz/drivers/zookeeper.py b/tooz/drivers/zookeeper.py
index 07cf0d2..e64c19b 100644
--- a/tooz/drivers/zookeeper.py
+++ b/tooz/drivers/zookeeper.py
@@ -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)
diff --git a/tooz/utils.py b/tooz/utils.py
new file mode 100644
index 0000000..8720d7d
--- /dev/null
+++ b/tooz/utils.py
@@ -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)