diff --git a/glance/common/exception.py b/glance/common/exception.py index 9fa9b1a3..b34d8c06 100644 --- a/glance/common/exception.py +++ b/glance/common/exception.py @@ -36,23 +36,15 @@ class GlanceException(Exception): """ message = _("An unknown exception occurred") - def __init__(self, *args, **kwargs): + def __init__(self, message=None, *args, **kwargs): + if not message: + message = self.message try: - self._error_string = self.message % kwargs + message = message % kwargs except Exception: # at least get the core message out if something happened - self._error_string = self.message - if len(args) > 0: - # If there is a non-kwarg parameter, assume it's the error - # message or reason description and tack it on to the end - # of the exception message - # Convert all arguments into their string representations... - args = ["%s" % arg for arg in args] - self._error_string = (self._error_string + - "\nDetails: %s" % '\n'.join(args)) - - def __str__(self): - return self._error_string + pass + super(GlanceException, self).__init__(message) class MissingArgumentError(GlanceException): diff --git a/glance/db/sqlalchemy/migrate_repo/versions/015_quote_swift_credentials.py b/glance/db/sqlalchemy/migrate_repo/versions/015_quote_swift_credentials.py index 653d7c7a..ff234df8 100644 --- a/glance/db/sqlalchemy/migrate_repo/versions/015_quote_swift_credentials.py +++ b/glance/db/sqlalchemy/migrate_repo/versions/015_quote_swift_credentials.py @@ -109,7 +109,7 @@ def legacy_parse_uri(self, uri): "like so: " "swift+http://user:pass@authurl.com/v1/container/obj" ) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) pieces = urlparse.urlparse(uri) assert pieces.scheme in ('swift', 'swift+http', 'swift+https') @@ -140,7 +140,7 @@ def legacy_parse_uri(self, uri): if len(cred_parts) == 1: reason = (_("Badly formed credentials '%(creds)s' in Swift " "URI") % locals()) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) elif len(cred_parts) == 3: user = ':'.join(cred_parts[0:2]) else: @@ -160,4 +160,4 @@ def legacy_parse_uri(self, uri): self.authurl = '/'.join(path_parts) except IndexError: reason = _("Badly formed Swift URI") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) diff --git a/glance/store/filesystem.py b/glance/store/filesystem.py index 9374d853..4747fb58 100644 --- a/glance/store/filesystem.py +++ b/glance/store/filesystem.py @@ -63,7 +63,7 @@ class StoreLocation(glance.store.location.StoreLocation): path = (pieces.netloc + pieces.path).strip() if path == '': reason = _("No path specified") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) self.path = path diff --git a/glance/store/http.py b/glance/store/http.py index 96a26900..9a832cad 100644 --- a/glance/store/http.py +++ b/glance/store/http.py @@ -75,12 +75,12 @@ class StoreLocation(glance.store.location.StoreLocation): except ValueError: reason = (_("Credentials '%s' not well-formatted.") % "".join(creds)) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) else: self.user = None if netloc == '': reason = _("No address specified in HTTP URL") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) self.netloc = netloc self.path = path diff --git a/glance/store/rbd.py b/glance/store/rbd.py index 9e8494fb..9dd96d0a 100644 --- a/glance/store/rbd.py +++ b/glance/store/rbd.py @@ -70,7 +70,8 @@ class StoreLocation(glance.store.location.StoreLocation): def parse_uri(self, uri): if not uri.startswith('rbd://'): - raise exception.BadStoreUri(uri, _('URI must start with rbd://')) + raise exception.BadStoreUri(uri=uri, + reason=_('URI must start with rbd://')) self.image = uri[6:] diff --git a/glance/store/s3.py b/glance/store/s3.py index 6a79e605..4311de65 100644 --- a/glance/store/s3.py +++ b/glance/store/s3.py @@ -111,7 +111,7 @@ class StoreLocation(glance.store.location.StoreLocation): "s3+https://accesskey:secretkey@s3.amazonaws.com/bucket/" "key-id" ) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) pieces = urlparse.urlparse(uri) assert pieces.scheme in ('s3', 's3+http', 's3+https') @@ -137,7 +137,7 @@ class StoreLocation(glance.store.location.StoreLocation): self.secretkey = secret_key except IndexError: reason = _("Badly formed S3 credentials %s") % creds - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) else: self.accesskey = None path = entire_path @@ -149,10 +149,10 @@ class StoreLocation(glance.store.location.StoreLocation): self.s3serviceurl = '/'.join(path_parts).strip('/') else: reason = _("Badly formed S3 URI. Missing s3 service URL.") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) except IndexError: reason = _("Badly formed S3 URI") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) class ChunkedFile(object): diff --git a/glance/store/swift.py b/glance/store/swift.py index 9159198d..61f5f701 100644 --- a/glance/store/swift.py +++ b/glance/store/swift.py @@ -128,7 +128,7 @@ class StoreLocation(glance.store.location.StoreLocation): "like so: " "swift+http://user:pass@authurl.com/v1/container/obj" ) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) pieces = urlparse.urlparse(uri) assert pieces.scheme in ('swift', 'swift+http', 'swift+https') @@ -155,7 +155,7 @@ class StoreLocation(glance.store.location.StoreLocation): if len(cred_parts) != 2: reason = (_("Badly formed credentials '%(creds)s' in Swift " "URI") % locals()) - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) user, key = cred_parts self.user = urllib.unquote(user) self.key = urllib.unquote(key) @@ -171,7 +171,7 @@ class StoreLocation(glance.store.location.StoreLocation): self.authurl = '/'.join(path_parts) except IndexError: reason = _("Badly formed Swift URI") - raise exception.BadStoreUri(uri, reason) + raise exception.BadStoreUri(uri=uri, reason=reason) @property def swift_auth_url(self): diff --git a/glance/tests/unit/common/test_exception.py b/glance/tests/unit/common/test_exception.py new file mode 100644 index 00000000..fefe81e1 --- /dev/null +++ b/glance/tests/unit/common/test_exception.py @@ -0,0 +1,44 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack, LLC +# 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 glance.common import exception +from glance.tests import utils as test_utils + + +class GlanceExceptionTestCase(test_utils.BaseTestCase): + + def test_default_error_msg(self): + class FakeGlanceException(exception.GlanceException): + message = "default message" + + exc = FakeGlanceException() + self.assertEquals(unicode(exc), 'default message') + + def test_specified_error_msg(self): + self.assertTrue('test' in unicode(exception.GlanceException('test'))) + + def test_default_error_msg_with_kwargs(self): + class FakeGlanceException(exception.GlanceException): + message = "default message: %(code)s" + + exc = FakeGlanceException(code=500) + self.assertEquals(unicode(exc), "default message: 500") + + def test_specified_error_msg_with_kwargs(self): + self.assertTrue('test: 500' in + unicode(exception.GlanceException('test: %(code)s', + code=500))) diff --git a/glance/tests/unit/test_misc.py b/glance/tests/unit/test_misc.py index 922b8a1d..d4e151eb 100644 --- a/glance/tests/unit/test_misc.py +++ b/glance/tests/unit/test_misc.py @@ -21,7 +21,6 @@ import datetime import re from glance.common import crypt -from glance.common import exception from glance.common import utils from glance.openstack.common import importutils from glance.tests import utils as test_utils