Revert "Reuse exceptions from Oslo"

This reverts commit 3bde9c3f42.

This change is a part of a series that is not backwards compatible.
It broke applications using the client library.

Change-Id: Id01a0a0feb601539d3855f445ebaf6a3cc000610
Partial-Bug: #1340596
This commit is contained in:
Russell Bryant 2014-07-11 13:31:14 -04:00
parent e14f9741a6
commit d678dd2955
5 changed files with 169 additions and 25 deletions

View File

@ -16,31 +16,161 @@
Exception definitions. Exception definitions.
""" """
import inspect
import sys
import six class UnsupportedVersion(Exception):
"""Indicates that the user is trying to use an unsupported
from novaclient.openstack.common.apiclient.exceptions import * # noqa version of the API.
"""
# NOTE(akurilin): Since v.2.17.0 this alias should be left here to support pass
# backwards compatibility.
OverLimit = RequestEntityTooLarge
class NoTokenLookupException(ClientException): class CommandError(Exception):
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 RateLimit(RequestEntityTooLarge): class ClientException(Exception):
"""
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.
""" """
@ -48,11 +178,25 @@ class RateLimit(RequestEntityTooLarge):
message = "Rate limit" message = "Rate limit"
_code_map = dict( # NotImplemented is a python keyword.
(getattr(obj, 'http_status', None), obj) class HTTPNotImplemented(ClientException):
for name, obj in six.iteritems(vars(sys.modules[__name__])) """
if inspect.isclass(obj) and getattr(obj, 'http_status', False) HTTP 501 - Not Implemented: the server does not support this operation.
) """
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)
class InvalidUsage(RuntimeError): class InvalidUsage(RuntimeError):
@ -77,7 +221,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 = {
'http_status': response.status_code, 'code': 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.ServiceUnavailable as exc: except exceptions.ClientException as exc:
self.assertIn('Service Unavailable (HTTP 503)', six.text_type(exc)) self.assertIn('Unknown Error', six.text_type(exc))
else: else:
self.fail('Expected exceptions.ServiceUnavailable') self.fail('Expected exceptions.ClientException')

View File

@ -89,15 +89,15 @@ class FindResourceTestCase(test_utils.TestCase):
def test_find_none(self): def test_find_none(self):
"""Test a few non-valid inputs.""" """Test a few non-valid inputs."""
self.assertRaises(exceptions.ResourceNotFound, self.assertRaises(exceptions.CommandError,
utils.find_resource, utils.find_resource,
self.manager, self.manager,
'asdf') 'asdf')
self.assertRaises(exceptions.ResourceNotFound, self.assertRaises(exceptions.CommandError,
utils.find_resource, utils.find_resource,
self.manager, self.manager,
None) None)
self.assertRaises(exceptions.ResourceNotFound, self.assertRaises(exceptions.CommandError,
utils.find_resource, utils.find_resource,
self.manager, self.manager,
{}) {})

View File

@ -990,7 +990,7 @@ class ShellTest(utils.TestCase):
self.assert_called('GET', '/flavors/1', pos=-1) self.assert_called('GET', '/flavors/1', pos=-1)
def test_show_bad_id(self): def test_show_bad_id(self):
self.assertRaises(exceptions.ResourceNotFound, self.assertRaises(exceptions.CommandError,
self.run_command, 'show xxx') self.run_command, 'show xxx')
@mock.patch('novaclient.v1_1.shell.utils.print_dict') @mock.patch('novaclient.v1_1.shell.utils.print_dict')

View File

@ -242,7 +242,7 @@ def find_resource(manager, name_or_id, **find_args):
msg = _("No %(class)s with a name or ID of '%(name)s' exists.") % \ msg = _("No %(class)s with a name or ID of '%(name)s' exists.") % \
{'class': manager.resource_class.__name__.lower(), {'class': manager.resource_class.__name__.lower(),
'name': name_or_id} 'name': name_or_id}
raise exceptions.ResourceNotFound(msg) raise exceptions.CommandError(msg)
except exceptions.NoUniqueMatch: except exceptions.NoUniqueMatch:
msg = (_("Multiple %(class)s matches found for '%(name)s', use an ID " msg = (_("Multiple %(class)s matches found for '%(name)s', use an ID "
"to be more specific.") % "to be more specific.") %