diff --git a/tooz/drivers/zookeeper.py b/tooz/drivers/zookeeper.py index d3fa981e..862403a5 100644 --- a/tooz/drivers/zookeeper.py +++ b/tooz/drivers/zookeeper.py @@ -19,7 +19,9 @@ from kazoo import exceptions from kazoo.protocol import paths from zake import fake_client + from tooz import coordination +from tooz import utils _TOOZ_NAMESPACE = "tooz" @@ -49,7 +51,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 = "/%s/%s" % (_TOOZ_NAMESPACE, group_id) @@ -62,12 +64,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver): try: async_result.get(block=True, timeout=timeout) except exceptions.NodeExistsError: - raise coordination.MemberAlreadyExist(str(member_id)) + raise coordination.MemberAlreadyExist("member '%s' already exists" + % (member_id)) except exceptions.NoNodeError: raise coordination.GroupNotCreated("group '%s' has not been " "created" % 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) @@ -87,7 +90,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver): "has not been created" % member_id, group_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) @@ -103,7 +106,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver): raise coordination.GroupNotCreated("group '%s' does not exist" % group_id) except exceptions.ZookeeperError as e: - raise coordination.ToozError(str(e)) + raise coordination.ToozError(utils.exception_message(e)) else: return members_ids @@ -124,7 +127,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver): "has not been created" % (member_id, group_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) @@ -143,7 +146,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver): "has not been created" % (member_id, group_id)) except exceptions.ZookeeperError as e: - raise coordination.ToozError(str(e)) + raise coordination.ToozError(utils.exception_message(e)) else: return capabilities @@ -161,7 +164,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 group_ids diff --git a/tooz/utils.py b/tooz/utils.py new file mode 100644 index 00000000..5ab3b689 --- /dev/null +++ b/tooz/utils.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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)