diff --git a/glance_store/_drivers/filesystem.py b/glance_store/_drivers/filesystem.py index 9b2ed505..d23fc30b 100644 --- a/glance_store/_drivers/filesystem.py +++ b/glance_store/_drivers/filesystem.py @@ -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))) diff --git a/glance_store/_drivers/s3.py b/glance_store/_drivers/s3.py index 12e0f563..0ad9777c 100644 --- a/glance_store/_drivers/s3.py +++ b/glance_store/_drivers/s3.py @@ -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 " diff --git a/glance_store/_drivers/swift/store.py b/glance_store/_drivers/swift/store.py index 603a4351..42ee9995 100644 --- a/glance_store/_drivers/swift/store.py +++ b/glance_store/_drivers/swift/store.py @@ -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 " diff --git a/glance_store/backend.py b/glance_store/backend.py index 205df5e0..ca699dde 100644 --- a/glance_store/backend.py +++ b/glance_store/backend.py @@ -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) diff --git a/glance_store/common/utils.py b/glance_store/common/utils.py index 3ade9533..9bdb3878 100644 --- a/glance_store/common/utils.py +++ b/glance_store/common/utils.py @@ -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 diff --git a/glance_store/driver.py b/glance_store/driver.py index bab94d89..28f45144 100644 --- a/glance_store/driver.py +++ b/glance_store/driver.py @@ -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 diff --git a/glance_store/tests/unit/test_swift_store.py b/glance_store/tests/unit/test_swift_store.py index f1f8769f..5f324250 100644 --- a/glance_store/tests/unit/test_swift_store.py +++ b/glance_store/tests/unit/test_swift_store.py @@ -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) diff --git a/glance_store/tests/unit/test_utils.py b/glance_store/tests/unit/test_utils.py deleted file mode 100644 index e3a40424..00000000 --- a/glance_store/tests/unit/test_utils.py +++ /dev/null @@ -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')