1
# Copyright 2010 Jacob Kaplan-Moss
7
class UnsupportedVersion(Exception):
8
"""Indicates that the user is trying to use an unsupported
13
class CommandError(Exception):
17
class AuthorizationFailure(Exception):
21
class NoUniqueMatch(Exception):
25
class NoTokenLookupException(Exception):
26
"""This form of authentication does not support looking up
27
endpoints from an existing token."""
31
class EndpointNotFound(Exception):
32
"""Could not find Service or Region in Service Catalog."""
36
class AmbiguousEndpoints(Exception):
37
"""Found more than one matching endpoint in Service Catalog."""
38
def __init__(self, endpoints=None):
39
self.endpoints = endpoints
42
return "AmbiguousEndpoints: %s" % repr(self.endpoints)
45
class ClientException(Exception):
47
The base exception class for all exceptions this library raises.
49
def __init__(self, code, message=None, details=None, request_id=None):
51
self.message = message or self.__class__.message
52
self.details = details
53
self.request_id = request_id
56
formatted_string = "%s (HTTP %s)" % (self.message, self.code)
58
formatted_string += " (Request-ID: %s)" % self.request_id
60
return formatted_string
63
class BadRequest(ClientException):
65
HTTP 400 - Bad request: you sent some malformed data.
68
message = "Bad request"
71
class Unauthorized(ClientException):
73
HTTP 401 - Unauthorized: bad credentials.
76
message = "Unauthorized"
79
class Forbidden(ClientException):
81
HTTP 403 - Forbidden: your credentials don't give you access to this
88
class NotFound(ClientException):
96
class OverLimit(ClientException):
98
HTTP 413 - Over limit: you're over the API limits for this time period.
101
message = "Over limit"
104
# NotImplemented is a python keyword.
105
class HTTPNotImplemented(ClientException):
107
HTTP 501 - Not Implemented: the server does not support this operation.
110
message = "Not Implemented"
113
# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__()
115
# _code_map = dict((c.http_status, c)
116
# for c in ClientException.__subclasses__())
118
# Instead, we have to hardcode it:
119
_code_map = dict((c.http_status, c) for c in [BadRequest, Unauthorized,
121
OverLimit, HTTPNotImplemented])
124
def from_response(response, body):
126
Return an instance of an ClientException or subclass
127
based on an httplib2 response.
131
resp, body = http.request(...)
132
if resp.status != 200:
133
raise exception_from_response(resp, body)
135
cls = _code_map.get(response.status, ClientException)
136
request_id = response.get('x-compute-request-id')
140
if hasattr(body, 'keys'):
141
error = body[body.keys()[0]]
142
message = error.get('message', None)
143
details = error.get('details', None)
144
return cls(code=response.status, message=message, details=details,
145
request_id=request_id)
147
return cls(code=response.status, request_id=request_id)