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
This commit is contained in:
parent
069c3d308d
commit
b0d8733298
@ -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
|
||||
|
||||
|
27
tooz/utils.py
Normal file
27
tooz/utils.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user