Reuse exceptions from Oslo

- Remove unused exceptions from novaclient.exceptions
- Reuse exceptions from common code

OpenStack clients which already use exception from common code:
- ironicclient   - I784007f36dd6a63fc4c77e0d0f0f000e05eb792f
- keystoneclient - Iedf4e5d753d4278d81751ba0f55fdef3566b56de
- saharaclient
- tuskarclient   - Iacbf219168fae62d864c6594dd41e0737e848cfa

Similar patches in other clients related to reusing exceptions
from common code:
- cinderclient   - I07f04ba5357e0196fe43b510e1d2fff5afa02faa
- glanceclient   - I9838391cff7e450f08d27668564b02e63259186c

Related to blueprint common-client-library-2

Change-Id: Ia35d65d57a0047f88a609070f8b4c76215cbd3e9
This commit is contained in:
Andrey Kurilin 2014-01-29 13:03:26 +02:00
parent 25987144ac
commit 3bde9c3f42
2 changed files with 20 additions and 164 deletions

View File

@ -16,161 +16,31 @@
Exception definitions. Exception definitions.
""" """
import inspect
import sys
class UnsupportedVersion(Exception): import six
"""Indicates that the user is trying to use an unsupported
version of the API. from novaclient.openstack.common.apiclient.exceptions import * # noqa
"""
pass # NOTE(akurilin): Since v.2.17.0 this alias should be left here to support
# backwards compatibility.
OverLimit = RequestEntityTooLarge
class CommandError(Exception): class NoTokenLookupException(ClientException):
pass
class AuthorizationFailure(Exception):
pass
class NoUniqueMatch(Exception):
pass
class AuthSystemNotFound(Exception):
"""When the user specify a AuthSystem but not installed."""
def __init__(self, auth_system):
self.auth_system = auth_system
def __str__(self):
return "AuthSystemNotFound: %s" % repr(self.auth_system)
class NoTokenLookupException(Exception):
"""This form of authentication does not support looking up """This form of authentication does not support looking up
endpoints from an existing token. endpoints from an existing token.
""" """
pass pass
class EndpointNotFound(Exception):
"""Could not find Service or Region in Service Catalog."""
pass
class AmbiguousEndpoints(Exception):
"""Found more than one matching endpoint in Service Catalog."""
def __init__(self, endpoints=None):
self.endpoints = endpoints
def __str__(self):
return "AmbiguousEndpoints: %s" % repr(self.endpoints)
class ConnectionRefused(Exception):
"""
Connection refused: the server refused the connection.
"""
def __init__(self, response=None):
self.response = response
def __str__(self):
return "ConnectionRefused: %s" % repr(self.response)
class InstanceInErrorState(Exception): class InstanceInErrorState(Exception):
"""Instance is in the error state.""" """Instance is in the error state."""
pass pass
class ClientException(Exception): class RateLimit(RequestEntityTooLarge):
"""
The base exception class for all exceptions this library raises.
"""
message = 'Unknown Error'
def __init__(self, code, message=None, details=None, request_id=None,
url=None, method=None):
self.code = code
self.message = message or self.__class__.message
self.details = details
self.request_id = request_id
self.url = url
self.method = method
def __str__(self):
formatted_string = "%s (HTTP %s)" % (self.message, self.code)
if self.request_id:
formatted_string += " (Request-ID: %s)" % self.request_id
return formatted_string
class BadRequest(ClientException):
"""
HTTP 400 - Bad request: you sent some malformed data.
"""
http_status = 400
message = "Bad request"
class Unauthorized(ClientException):
"""
HTTP 401 - Unauthorized: bad credentials.
"""
http_status = 401
message = "Unauthorized"
class Forbidden(ClientException):
"""
HTTP 403 - Forbidden: your credentials don't give you access to this
resource.
"""
http_status = 403
message = "Forbidden"
class NotFound(ClientException):
"""
HTTP 404 - Not found
"""
http_status = 404
message = "Not found"
class MethodNotAllowed(ClientException):
"""
HTTP 405 - Method Not Allowed
"""
http_status = 405
message = "Method Not Allowed"
class Conflict(ClientException):
"""
HTTP 409 - Conflict
"""
http_status = 409
message = "Conflict"
class OverLimit(ClientException):
"""
HTTP 413 - Over limit: you're over the API limits for this time period.
"""
http_status = 413
message = "Over limit"
def __init__(self, *args, **kwargs):
try:
self.retry_after = int(kwargs.pop('retry_after'))
except (KeyError, ValueError):
self.retry_after = 0
super(OverLimit, self).__init__(*args, **kwargs)
class RateLimit(OverLimit):
""" """
HTTP 429 - Rate limit: you've sent too many requests for this time period. HTTP 429 - Rate limit: you've sent too many requests for this time period.
""" """
@ -178,25 +48,11 @@ class RateLimit(OverLimit):
message = "Rate limit" message = "Rate limit"
# NotImplemented is a python keyword. _code_map = dict(
class HTTPNotImplemented(ClientException): (getattr(obj, 'http_status', None), obj)
""" for name, obj in six.iteritems(vars(sys.modules[__name__]))
HTTP 501 - Not Implemented: the server does not support this operation. if inspect.isclass(obj) and getattr(obj, 'http_status', False)
""" )
http_status = 501
message = "Not Implemented"
# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__()
# so we can do this:
# _code_map = dict((c.http_status, c)
# for c in ClientException.__subclasses__())
#
# Instead, we have to hardcode it:
_error_classes = [BadRequest, Unauthorized, Forbidden, NotFound,
MethodNotAllowed, Conflict, OverLimit, RateLimit,
HTTPNotImplemented]
_code_map = dict((c.http_status, c) for c in _error_classes)
def from_response(response, body, url, method=None): def from_response(response, body, url, method=None):
@ -211,7 +67,7 @@ def from_response(response, body, url, method=None):
raise exception_from_response(resp, rest.text) raise exception_from_response(resp, rest.text)
""" """
kwargs = { kwargs = {
'code': response.status_code, 'http_status': response.status_code,
'method': method, 'method': method,
'url': url, 'url': url,
'request_id': None, 'request_id': None,

View File

@ -150,7 +150,7 @@ class ClientTest(utils.TestCase):
# Python 2.7 and testtools doesn't match that implementation yet # Python 2.7 and testtools doesn't match that implementation yet
try: try:
cl.get('/hi') cl.get('/hi')
except exceptions.ClientException as exc: except exceptions.ServiceUnavailable as exc:
self.assertIn('Unknown Error', six.text_type(exc)) self.assertIn('Service Unavailable (HTTP 503)', six.text_type(exc))
else: else:
self.fail('Expected exceptions.ClientException') self.fail('Expected exceptions.ServiceUnavailable')