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
dfd0e7870e
@ -17,9 +17,12 @@
|
|||||||
from kazoo import client
|
from kazoo import client
|
||||||
from kazoo import exceptions
|
from kazoo import exceptions
|
||||||
from kazoo.protocol import paths
|
from kazoo.protocol import paths
|
||||||
|
import six
|
||||||
from zake import fake_client
|
from zake import fake_client
|
||||||
|
|
||||||
|
|
||||||
from tooz import coordination
|
from tooz import coordination
|
||||||
|
from tooz import utils
|
||||||
|
|
||||||
_TOOZ_NAMESPACE = "tooz"
|
_TOOZ_NAMESPACE = "tooz"
|
||||||
|
|
||||||
@ -49,7 +52,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
except exceptions.NoNodeError:
|
except exceptions.NoNodeError:
|
||||||
raise coordination.ToozError("tooz namespace has not been created")
|
raise coordination.ToozError("tooz namespace has not been created")
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
|
|
||||||
def create_group(self, group_id):
|
def create_group(self, group_id):
|
||||||
group_path = "/%s/%s" % (_TOOZ_NAMESPACE, group_id)
|
group_path = "/%s/%s" % (_TOOZ_NAMESPACE, group_id)
|
||||||
@ -62,12 +65,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
try:
|
try:
|
||||||
async_result.get(block=True, timeout=timeout)
|
async_result.get(block=True, timeout=timeout)
|
||||||
except exceptions.NodeExistsError:
|
except exceptions.NodeExistsError:
|
||||||
raise coordination.MemberAlreadyExist(str(member_id))
|
raise coordination.MemberAlreadyExist("member '%s' already exists"
|
||||||
|
% (member_id))
|
||||||
except exceptions.NoNodeError:
|
except exceptions.NoNodeError:
|
||||||
raise coordination.GroupNotCreated("group '%s' has not been "
|
raise coordination.GroupNotCreated("group '%s' has not been "
|
||||||
"created" % group_id)
|
"created" % group_id)
|
||||||
except exceptions.ZookeeperError as e:
|
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""):
|
def join_group(self, group_id, capabilities=b""):
|
||||||
member_path = self._path_member(group_id, self._member_id)
|
member_path = self._path_member(group_id, self._member_id)
|
||||||
@ -87,7 +91,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
"has not been created" %
|
"has not been created" %
|
||||||
member_id, group_id)
|
member_id, group_id)
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
|
|
||||||
def leave_group(self, group_id):
|
def leave_group(self, group_id):
|
||||||
member_path = self._path_member(group_id, self._member_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" %
|
raise coordination.GroupNotCreated("group '%s' does not exist" %
|
||||||
group_id)
|
group_id)
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
else:
|
else:
|
||||||
return members_ids
|
return members_ids
|
||||||
|
|
||||||
@ -124,7 +128,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
"has not been created" %
|
"has not been created" %
|
||||||
(member_id, group_id))
|
(member_id, group_id))
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
|
|
||||||
def update_capabilities(self, group_id, capabilities):
|
def update_capabilities(self, group_id, capabilities):
|
||||||
member_path = self._path_member(group_id, self._member_id)
|
member_path = self._path_member(group_id, self._member_id)
|
||||||
@ -143,7 +147,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
"has not been created" %
|
"has not been created" %
|
||||||
(member_id, group_id))
|
(member_id, group_id))
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
else:
|
else:
|
||||||
return capabilities
|
return capabilities
|
||||||
|
|
||||||
@ -161,7 +165,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||||||
except exceptions.NoNodeError:
|
except exceptions.NoNodeError:
|
||||||
raise coordination.ToozError("tooz namespace has not been created")
|
raise coordination.ToozError("tooz namespace has not been created")
|
||||||
except exceptions.ZookeeperError as e:
|
except exceptions.ZookeeperError as e:
|
||||||
raise coordination.ToozError(str(e))
|
raise coordination.ToozError(utils.exception_message(e))
|
||||||
else:
|
else:
|
||||||
return group_ids
|
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