Use oslo_utils.encodeutils.exception_to_unicode()
Replace glance_store.common.utils.exception_to_str() with encodeutils.exception_to_unicode(). exception_to_unicode() tries more options to get a nice error message as Unicode from an exception object. As exception_to_str(), it catches and handles exceptions. Remove glance_store.common.utils.exception_to_str() and related unit tests. The function is not part of the store API and Glance does not use it. glance already uses exception_to_unicode(): see change I86008c8adc0c5664f96573c1015cc15e2d06e3e2. By the way, glance did not use exception_to_str() from glance_store but had its own copy of the function (which was removed by the mentioned change). Change-Id: I52cab2e773c257e36d36290f6060811f83f18bb0
This commit is contained in:
parent
8a7729110a
commit
fc876c97db
@ -27,6 +27,7 @@ import stat
|
||||
import jsonschema
|
||||
from oslo_config import cfg
|
||||
from oslo_serialization import jsonutils
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import excutils
|
||||
from oslo_utils import units
|
||||
from six.moves import urllib
|
||||
@ -271,18 +272,20 @@ class Store(glance_store.driver.Store):
|
||||
self.FILESYSTEM_STORE_METADATA = metadata
|
||||
except (jsonschema.exceptions.ValidationError,
|
||||
exceptions.BackendException, ValueError) as vee:
|
||||
err_msg = encodeutils.exception_to_unicode(vee)
|
||||
reason = _('The JSON in the metadata file %(file)s is '
|
||||
'not valid and it can not be used: '
|
||||
'%(vee)s.') % dict(file=metadata_file,
|
||||
vee=utils.exception_to_str(vee))
|
||||
vee=err_msg)
|
||||
LOG.error(reason)
|
||||
raise exceptions.BadStoreConfiguration(
|
||||
store_name="filesystem", reason=reason)
|
||||
except IOError as ioe:
|
||||
err_msg = encodeutils.exception_to_unicode(ioe)
|
||||
reason = _('The path for the metadata file %(file)s could '
|
||||
'not be accessed: '
|
||||
'%(ioe)s.') % dict(file=metadata_file,
|
||||
ioe=utils.exception_to_str(ioe))
|
||||
ioe=err_msg)
|
||||
LOG.error(reason)
|
||||
raise exceptions.BadStoreConfiguration(
|
||||
store_name="filesystem", reason=reason)
|
||||
@ -635,4 +638,5 @@ class Store(glance_store.driver.Store):
|
||||
except Exception as e:
|
||||
msg = _('Unable to remove partial image '
|
||||
'data for image %(iid)s: %(e)s')
|
||||
LOG.error(msg % dict(iid=iid, e=utils.exception_to_str(e)))
|
||||
LOG.error(msg % dict(iid=iid,
|
||||
e=encodeutils.exception_to_unicode(e)))
|
||||
|
@ -24,6 +24,7 @@ import tempfile
|
||||
import debtcollector
|
||||
import eventlet
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import netutils
|
||||
from oslo_utils import units
|
||||
import six
|
||||
@ -769,7 +770,7 @@ def create_bucket_if_missing(conf, bucket, s3_conn):
|
||||
except S3ResponseError as e:
|
||||
msg = (_("Failed to add bucket to S3.\n"
|
||||
"Got error from S3: %s.") %
|
||||
utils.exception_to_str(e))
|
||||
encodeutils.exception_to_unicode(e))
|
||||
raise glance_store.BackendException(msg)
|
||||
else:
|
||||
msg = (_("The bucket %(bucket)s does not exist in "
|
||||
|
@ -20,6 +20,7 @@ import logging
|
||||
import math
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import excutils
|
||||
from oslo_utils import units
|
||||
import six
|
||||
@ -33,7 +34,6 @@ except ImportError:
|
||||
import glance_store
|
||||
from glance_store._drivers.swift import utils as sutils
|
||||
from glance_store import capabilities
|
||||
from glance_store.common import utils as cutils
|
||||
from glance_store import driver
|
||||
from glance_store import exceptions
|
||||
from glance_store import i18n
|
||||
@ -146,8 +146,8 @@ def swift_retry_iter(resp_iter, length, store, location, context):
|
||||
yield chunk
|
||||
bytes_read += len(chunk)
|
||||
except swiftclient.ClientException as e:
|
||||
LOG.warn(_("Swift exception raised %s") %
|
||||
cutils.exception_to_str(e))
|
||||
LOG.warn(_("Swift exception raised %s")
|
||||
% encodeutils.exception_to_unicode(e))
|
||||
|
||||
if bytes_read != length:
|
||||
if retries == store.conf.glance_store.swift_store_retry_get_count:
|
||||
@ -615,7 +615,8 @@ class BaseStore(driver.Store):
|
||||
raise exceptions.Duplicate(message=msg)
|
||||
|
||||
msg = (_(u"Failed to add object to Swift.\n"
|
||||
"Got error from Swift: %s.") % cutils.exception_to_str(e))
|
||||
"Got error from Swift: %s.")
|
||||
% encodeutils.exception_to_unicode(e))
|
||||
LOG.error(msg)
|
||||
raise glance_store.BackendException(msg)
|
||||
|
||||
@ -696,8 +697,8 @@ class BaseStore(driver.Store):
|
||||
connection.put_container(container)
|
||||
except swiftclient.ClientException as e:
|
||||
msg = (_("Failed to add container to Swift.\n"
|
||||
"Got error from Swift: %s.") %
|
||||
cutils.exception_to_str(e))
|
||||
"Got error from Swift: %s.")
|
||||
% encodeutils.exception_to_unicode(e))
|
||||
raise glance_store.BackendException(msg)
|
||||
else:
|
||||
msg = (_("The container %(container)s does not exist in "
|
||||
|
@ -16,12 +16,12 @@
|
||||
import logging
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import encodeutils
|
||||
import six
|
||||
from stevedore import driver
|
||||
from stevedore import extension
|
||||
|
||||
from glance_store import capabilities
|
||||
from glance_store.common import utils
|
||||
from glance_store import exceptions
|
||||
from glance_store import i18n
|
||||
from glance_store import location
|
||||
@ -352,9 +352,9 @@ def store_add_to_backend(image_id, data, size, store, context=None):
|
||||
except exceptions.BackendException as e:
|
||||
e_msg = (_("A bad metadata structure was returned from the "
|
||||
"%(driver)s storage driver: %(metadata)s. %(e)s.") %
|
||||
dict(driver=utils.exception_to_str(store),
|
||||
metadata=utils.exception_to_str(metadata),
|
||||
e=utils.exception_to_str(e)))
|
||||
dict(driver=encodeutils.exception_to_unicode(store),
|
||||
metadata=encodeutils.exception_to_unicode(metadata),
|
||||
e=encodeutils.exception_to_unicode(e)))
|
||||
LOG.error(e_msg)
|
||||
raise exceptions.BackendException(e_msg)
|
||||
return (location, size, checksum, metadata)
|
||||
|
@ -25,8 +25,6 @@ try:
|
||||
from eventlet import sleep
|
||||
except ImportError:
|
||||
from time import sleep
|
||||
from oslo_utils import encodeutils
|
||||
import six
|
||||
|
||||
from glance_store.i18n import _
|
||||
|
||||
@ -141,17 +139,3 @@ class CooperativeReader(object):
|
||||
|
||||
def __iter__(self):
|
||||
return cooperative_iter(self.fd.__iter__())
|
||||
|
||||
|
||||
def exception_to_str(exc):
|
||||
try:
|
||||
error = six.text_type(exc)
|
||||
except UnicodeError:
|
||||
try:
|
||||
error = str(exc)
|
||||
except UnicodeError:
|
||||
error = ("Caught '%(exception)s' exception." %
|
||||
{"exception": exc.__class__.__name__})
|
||||
if six.PY2:
|
||||
error = encodeutils.safe_encode(error, errors='ignore')
|
||||
return error
|
||||
|
@ -19,11 +19,11 @@
|
||||
import logging
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import importutils
|
||||
from oslo_utils import units
|
||||
|
||||
from glance_store import capabilities
|
||||
from glance_store.common import utils
|
||||
from glance_store import exceptions
|
||||
from glance_store import i18n
|
||||
|
||||
@ -67,7 +67,8 @@ class Store(capabilities.StoreCapability):
|
||||
except exceptions.BadStoreConfiguration as e:
|
||||
self.unset_capabilities(capabilities.BitMasks.WRITE_ACCESS)
|
||||
msg = (_(u"Failed to configure store correctly: %s "
|
||||
"Disabling add method.") % utils.exception_to_str(e))
|
||||
"Disabling add method.")
|
||||
% encodeutils.exception_to_unicode(e))
|
||||
LOG.warn(msg)
|
||||
if re_raise_bsc:
|
||||
raise
|
||||
|
@ -23,6 +23,7 @@ import tempfile
|
||||
import uuid
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import units
|
||||
from oslotest import moxstubout
|
||||
import requests_mock
|
||||
@ -37,7 +38,6 @@ from glance_store._drivers.swift import store as swift
|
||||
from glance_store import backend
|
||||
from glance_store import BackendException
|
||||
from glance_store import capabilities
|
||||
from glance_store.common import utils
|
||||
from glance_store import exceptions
|
||||
from glance_store import location
|
||||
from glance_store.tests import base
|
||||
@ -471,8 +471,8 @@ class SwiftTests(object):
|
||||
self.store.add(str(uuid.uuid4()), image_swift, 0)
|
||||
except BackendException as e:
|
||||
exception_caught = True
|
||||
self.assertIn("container noexist does not exist "
|
||||
"in Swift", utils.exception_to_str(e))
|
||||
self.assertIn("container noexist does not exist in Swift",
|
||||
encodeutils.exception_to_unicode(e))
|
||||
self.assertTrue(exception_caught)
|
||||
self.assertEqual(SWIFT_PUT_OBJECT_CALLS, 0)
|
||||
|
||||
@ -601,7 +601,7 @@ class SwiftTests(object):
|
||||
exception_caught = True
|
||||
expected_msg = "container %s does not exist in Swift"
|
||||
expected_msg = expected_msg % expected_container
|
||||
self.assertIn(expected_msg, utils.exception_to_str(e))
|
||||
self.assertIn(expected_msg, encodeutils.exception_to_unicode(e))
|
||||
self.assertTrue(exception_caught)
|
||||
self.assertEqual(SWIFT_PUT_OBJECT_CALLS, 0)
|
||||
|
||||
|
@ -1,44 +0,0 @@
|
||||
# Copyright 2014 OpenStack Foundation
|
||||
# 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.
|
||||
|
||||
from oslotest import base
|
||||
import six
|
||||
|
||||
from glance_store.common import utils
|
||||
|
||||
|
||||
class TestUtils(base.BaseTestCase):
|
||||
"""Test routines in glance_store.common.utils."""
|
||||
|
||||
def test_exception_to_str(self):
|
||||
class FakeException(Exception):
|
||||
def __str__(self):
|
||||
raise UnicodeError()
|
||||
|
||||
ret = utils.exception_to_str(Exception('error message'))
|
||||
self.assertEqual(ret, 'error message')
|
||||
|
||||
ret = utils.exception_to_str(FakeException('\xa5 error message'))
|
||||
self.assertEqual(ret, "Caught '%(exception)s' exception." %
|
||||
{'exception': 'FakeException'})
|
||||
|
||||
def test_exception_to_str_ignore(self):
|
||||
if six.PY3:
|
||||
# On Python 3, exception messages are unicode strings, they are not
|
||||
# decoded from an encoding and so it's not possible to test the
|
||||
# "ignore" error handler
|
||||
self.skipTest("test specific to Python 2")
|
||||
ret = utils.exception_to_str(Exception('\xa5 error message'))
|
||||
self.assertEqual(ret, ' error message')
|
Loading…
Reference in New Issue
Block a user