utils: replace exception_message by exception_to_unicode
This leverages directly oslo_utils.encodeutils. Change-Id: Id345f4c18cfe98e7359c8934b7148c97e2e1fd99
This commit is contained in:
parent
de9308e154
commit
8d6540f6a9
|
@ -14,6 +14,7 @@
|
|||
|
||||
import logging
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import timeutils
|
||||
import requests
|
||||
import six
|
||||
|
@ -36,7 +37,7 @@ def _translate_failures(func):
|
|||
return func(*args, **kwargs)
|
||||
except requests.exceptions.RequestException as e:
|
||||
coordination.raise_with_cause(coordination.ToozConnectionError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
return wrapper
|
||||
|
@ -175,7 +176,8 @@ class EtcdDriver(coordination.CoordinationDriver):
|
|||
try:
|
||||
self.client.self_stats()
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
raise coordination.ToozConnectionError(utils.exception_message(e))
|
||||
raise coordination.ToozConnectionError(
|
||||
encodeutils.exception_to_unicode(e))
|
||||
|
||||
def get_lock(self, name):
|
||||
return EtcdLock(name, self, self.client, self.lock_timeout)
|
||||
|
|
|
@ -28,6 +28,7 @@ from concurrent import futures
|
|||
|
||||
import fasteners
|
||||
import jsonschema
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import timeutils
|
||||
import six
|
||||
|
||||
|
@ -51,7 +52,7 @@ def _translate_failures():
|
|||
yield
|
||||
except (EnvironmentError, jsonschema.ValidationError) as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
|
||||
|
@ -473,7 +474,7 @@ class FileFutureResult(coordination.CoordAsyncResult):
|
|||
return self._fut.result(timeout=timeout)
|
||||
except futures.TimeoutError as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def done(self):
|
||||
|
|
|
@ -20,6 +20,7 @@ import time
|
|||
|
||||
from concurrent import futures
|
||||
import msgpack
|
||||
from oslo_utils import encodeutils
|
||||
import six
|
||||
import sysv_ipc
|
||||
|
||||
|
@ -243,7 +244,7 @@ class IPCFutureResult(coordination.CoordAsyncResult):
|
|||
return self._fut.result(timeout=timeout)
|
||||
except futures.TimeoutError as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def done(self):
|
||||
|
|
|
@ -20,6 +20,7 @@ import logging
|
|||
import socket
|
||||
|
||||
from concurrent import futures
|
||||
from oslo_utils import encodeutils
|
||||
from pymemcache import client as pymemcache_client
|
||||
import six
|
||||
|
||||
|
@ -44,14 +45,14 @@ def _translate_failures(func):
|
|||
return func(*args, **kwargs)
|
||||
except pymemcache_client.MemcacheUnexpectedCloseError as e:
|
||||
coordination.raise_with_cause(coordination.ToozConnectionError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except (socket.timeout, socket.error,
|
||||
socket.gaierror, socket.herror) as e:
|
||||
# TODO(harlowja): get upstream pymemcache to produce a better
|
||||
# exception for these, using socket (vs. a memcache specific
|
||||
# error) seems sorta not right and/or the best approach...
|
||||
msg = utils.exception_message(e)
|
||||
msg = encodeutils.exception_to_unicode(e)
|
||||
if e.errno is not None:
|
||||
msg += " (with errno %s [%s])" % (errno.errorcode[e.errno],
|
||||
e.errno)
|
||||
|
@ -59,7 +60,7 @@ def _translate_failures(func):
|
|||
msg, cause=e)
|
||||
except pymemcache_client.MemcacheError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
return wrapper
|
||||
|
@ -533,9 +534,10 @@ class MemcachedFutureResult(coordination.CoordAsyncResult):
|
|||
try:
|
||||
return self._fut.result(timeout=timeout)
|
||||
except futures.TimeoutError as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
cause=e)
|
||||
coordination.raise_with_cause(
|
||||
coordination.OperationTimedOut,
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def done(self):
|
||||
return self._fut.done()
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
# under the License.
|
||||
import logging
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
import pymysql
|
||||
|
||||
import tooz
|
||||
|
@ -62,9 +63,10 @@ class MySQLLock(locking.Lock):
|
|||
self.acquired = True
|
||||
return True
|
||||
except pymysql.MySQLError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
cause=e)
|
||||
coordination.raise_with_cause(
|
||||
coordination.ToozError,
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
if blocking:
|
||||
raise _retry.Retry
|
||||
|
@ -83,7 +85,7 @@ class MySQLLock(locking.Lock):
|
|||
return True
|
||||
except pymysql.MySQLError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def __del__(self):
|
||||
|
@ -166,5 +168,5 @@ class MySQLDriver(coordination.CoordinationDriver):
|
|||
database=dbname)
|
||||
except (pymysql.err.OperationalError, pymysql.err.InternalError) as e:
|
||||
coordination.raise_with_cause(coordination.ToozConnectionError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
|
|
@ -18,6 +18,7 @@ import contextlib
|
|||
import hashlib
|
||||
import logging
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
import psycopg2
|
||||
import six
|
||||
|
||||
|
@ -55,7 +56,8 @@ _DIAGNOSTICS_ATTRS = tuple([
|
|||
|
||||
def _format_exception(e):
|
||||
lines = [
|
||||
"%s: %s" % (type(e).__name__, utils.exception_message(e).strip()),
|
||||
"%s: %s" % (type(e).__name__,
|
||||
encodeutils.exception_to_unicode(e).strip()),
|
||||
]
|
||||
if hasattr(e, 'pgcode') and e.pgcode is not None:
|
||||
lines.append("Error code: %s" % e.pgcode)
|
||||
|
|
|
@ -22,6 +22,7 @@ import logging
|
|||
import string
|
||||
|
||||
from concurrent import futures
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import strutils
|
||||
import redis
|
||||
from redis import exceptions
|
||||
|
@ -46,11 +47,11 @@ def _translate_failures():
|
|||
yield
|
||||
except (exceptions.ConnectionError, exceptions.TimeoutError) as e:
|
||||
coordination.raise_with_cause(coordination.ToozConnectionError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.RedisError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
|
||||
|
@ -415,7 +416,7 @@ return 1
|
|||
self.timeout)
|
||||
except exceptions.RedisError as e:
|
||||
coordination.raise_with_cause(coordination.ToozConnectionError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
else:
|
||||
# Ensure that the server is alive and not dead, this does not
|
||||
|
@ -779,7 +780,7 @@ class RedisFutureResult(coordination.CoordAsyncResult):
|
|||
return self._fut.result(timeout=timeout)
|
||||
except futures.TimeoutError as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def done(self):
|
||||
|
|
|
@ -22,6 +22,7 @@ from kazoo import exceptions
|
|||
from kazoo.handlers import eventlet as eventlet_handler
|
||||
from kazoo.handlers import threading as threading_handler
|
||||
from kazoo.protocol import paths
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import strutils
|
||||
import six
|
||||
from six.moves import filter as compat_filter
|
||||
|
@ -124,7 +125,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NodeExistsError:
|
||||
raise coordination.GroupAlreadyExist(group_id)
|
||||
|
@ -132,7 +133,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
raise coordination.ToozError("tooz namespace has not been created")
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def create_group(self, group_id):
|
||||
|
@ -149,7 +150,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.GroupNotCreated(group_id)
|
||||
|
@ -157,7 +158,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
raise coordination.GroupNotEmpty(group_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def delete_group(self, group_id):
|
||||
|
@ -174,7 +175,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NodeExistsError:
|
||||
raise coordination.MemberAlreadyExist(group_id, member_id)
|
||||
|
@ -182,7 +183,7 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
raise coordination.GroupNotCreated(group_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def join_group(self, group_id, capabilities=b""):
|
||||
|
@ -202,13 +203,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.MemberNotJoined(group_id, member_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def leave_group(self, group_id):
|
||||
|
@ -225,13 +226,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
members_ids = async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.GroupNotCreated(group_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
else:
|
||||
return set(m.encode('ascii') for m in members_ids)
|
||||
|
@ -250,13 +251,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.MemberNotJoined(group_id, member_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
def update_capabilities(self, group_id, capabilities):
|
||||
|
@ -275,13 +276,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
capabilities = async_result.get(block=True, timeout=timeout)[0]
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.MemberNotJoined(group_id, member_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
else:
|
||||
return cls._loads(capabilities)
|
||||
|
@ -303,13 +304,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.MemberNotJoined(group_id, member_id)
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
else:
|
||||
member_info = {
|
||||
|
@ -333,13 +334,13 @@ class BaseZooKeeperDriver(coordination.CoordinationDriver):
|
|||
group_ids = async_result.get(block=True, timeout=timeout)
|
||||
except timeout_exception as e:
|
||||
coordination.raise_with_cause(coordination.OperationTimedOut,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
except exceptions.NoNodeError:
|
||||
raise coordination.ToozError("tooz namespace has not been created")
|
||||
except exceptions.ZookeeperError as e:
|
||||
coordination.raise_with_cause(coordination.ToozError,
|
||||
utils.exception_message(e),
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
else:
|
||||
return set(g.encode('ascii') for g in group_ids)
|
||||
|
|
|
@ -18,11 +18,11 @@
|
|||
import os
|
||||
import uuid
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
import testtools
|
||||
from testtools import testcase
|
||||
|
||||
from tooz import coordination
|
||||
from tooz import utils
|
||||
|
||||
|
||||
@testtools.skipUnless(os.getenv("TOOZ_TEST_MYSQL_URL"),
|
||||
|
@ -37,7 +37,7 @@ class TestMYSQLDriver(testcase.TestCase):
|
|||
try:
|
||||
coord.stop()
|
||||
except coordination.ToozError as e:
|
||||
message = utils.exception_message(e)
|
||||
message = encodeutils.exception_to_unicode(e)
|
||||
if (message != 'Can not stop a driver which has not'
|
||||
' been started'):
|
||||
raise
|
||||
|
|
|
@ -22,11 +22,11 @@ try:
|
|||
except ImportError:
|
||||
import mock
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
import testtools
|
||||
from testtools import testcase
|
||||
|
||||
from tooz import coordination
|
||||
from tooz import utils
|
||||
|
||||
# Handle the case gracefully where the driver is not installed.
|
||||
try:
|
||||
|
@ -51,7 +51,7 @@ class TestPostgreSQLFailures(testcase.TestCase):
|
|||
except coordination.ToozError as e:
|
||||
# TODO(harlowja): make this better, so that we don't have to
|
||||
# do string checking...
|
||||
message = utils.exception_message(e)
|
||||
message = encodeutils.exception_to_unicode(e)
|
||||
if (message != 'Can not stop a driver which has not'
|
||||
' been started'):
|
||||
raise
|
||||
|
|
|
@ -157,11 +157,6 @@ def collapse(config, exclude=None, item_selector=None):
|
|||
return collapsed
|
||||
|
||||
|
||||
# TODO(harlowja): get rid of this...
|
||||
#: Return the string (unicode) representation of an exception.
|
||||
exception_message = encodeutils.exception_to_unicode
|
||||
|
||||
|
||||
def to_binary(text, encoding='ascii'):
|
||||
"""Return the binary representation of string (if not already binary)."""
|
||||
if not isinstance(text, six.binary_type):
|
||||
|
@ -174,7 +169,8 @@ def dumps(data, excp_cls=coordination.SerializationError):
|
|||
try:
|
||||
return msgpackutils.dumps(data)
|
||||
except (msgpack.PackException, ValueError) as e:
|
||||
coordination.raise_with_cause(excp_cls, exception_message(e),
|
||||
coordination.raise_with_cause(excp_cls,
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
|
||||
|
@ -183,7 +179,8 @@ def loads(blob, excp_cls=coordination.SerializationError):
|
|||
try:
|
||||
return msgpackutils.loads(blob)
|
||||
except (msgpack.UnpackException, ValueError) as e:
|
||||
coordination.raise_with_cause(excp_cls, exception_message(e),
|
||||
coordination.raise_with_cause(excp_cls,
|
||||
encodeutils.exception_to_unicode(e),
|
||||
cause=e)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue