1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
16
class BaseException(Exception):
17
"""An error occurred."""
18
def __init__(self, message=None):
19
self.message = message
22
return self.message or self.__class__.__doc__
25
class CommandError(BaseException):
26
"""Invalid usage of CLI"""
29
class InvalidEndpoint(BaseException):
30
"""The provided endpoint is invalid."""
33
class CommunicationError(BaseException):
34
"""Unable to communicate with server."""
37
class ClientException(Exception):
41
class HTTPException(ClientException):
42
"""Base exception for all HTTP-derived exceptions"""
45
def __init__(self, details=None):
46
self.details = details
49
return "%s (HTTP %s)" % (self.__class__.__name__, self.code)
52
class HTTPMultipleChoices(HTTPException):
56
self.details = ("Requested version of OpenStack Images API is not"
58
return "%s (HTTP %s) %s" % (self.__class__.__name__, self.code,
62
class BadRequest(HTTPException):
67
class HTTPBadRequest(BadRequest):
71
class Unauthorized(HTTPException):
76
class HTTPUnauthorized(Unauthorized):
80
class Forbidden(HTTPException):
85
class HTTPForbidden(Forbidden):
89
class NotFound(HTTPException):
94
class HTTPNotFound(NotFound):
98
class HTTPMethodNotAllowed(HTTPException):
102
class Conflict(HTTPException):
107
class HTTPConflict(Conflict):
111
class OverLimit(HTTPException):
116
class HTTPOverLimit(OverLimit):
120
class HTTPInternalServerError(HTTPException):
124
class HTTPNotImplemented(HTTPException):
128
class HTTPBadGateway(HTTPException):
132
class ServiceUnavailable(HTTPException):
137
class HTTPServiceUnavailable(ServiceUnavailable):
141
#NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
144
for obj_name in dir(sys.modules[__name__]):
145
if obj_name.startswith('HTTP'):
146
obj = getattr(sys.modules[__name__], obj_name)
147
_code_map[obj.code] = obj
150
def from_response(response):
151
"""Return an instance of an HTTPException based on httplib response."""
152
cls = _code_map.get(response.status, HTTPException)
156
class NoTokenLookupException(Exception):
161
class EndpointNotFound(Exception):