Add tests and simplify GlanceExceptions.
This also fixes some incorrect use of GlanceException parameters. Fixes bug: 1009122 Fixes bug: 1010140 Change-Id: Ic77856686a0259da2318e05b15f3d382508c00b9
This commit is contained in:
parent
575651883d
commit
e2c52ddfa6
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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:]
|
||||
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
44
glance/tests/unit/common/test_exception.py
Normal file
44
glance/tests/unit/common/test_exception.py
Normal file
@ -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)))
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user