From dfd0e7870e02c2b39602f511203ab7ef7fa59c1f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 4 Feb 2014 16:51:24 -0800 Subject: [PATCH] Handle str not working when exceptions contain unicode When a exception contains unicode, the usage of str() in python 3.3 will cause an error, so try to use the native python text_type first, and then only revert back to using str() if that fails. Change-Id: I4f36642453555c8e288cb67956d47bd0bcb8783f --- tooz/drivers/zookeeper.py | 20 ++++++++++++-------- tooz/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 tooz/utils.py diff --git a/tooz/drivers/zookeeper.py b/tooz/drivers/zookeeper.py index d3fa981e..c9865634 100644 --- a/tooz/drivers/zookeeper.py +++ b/tooz/drivers/zookeeper.py @@ -17,9 +17,12 @@ from kazoo import client from kazoo import exceptions from kazoo.protocol import paths +import six from zake import fake_client + from tooz import coordination +from tooz import utils _TOOZ_NAMESPACE = "tooz" @@ -49,7 +52,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 +65,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 +91,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 +107,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 +128,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 +147,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 +165,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)