| 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 |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 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 |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import sys |
| 14 |
|
| 15 |
from oslo_serialization import jsonutils |
| 16 |
|
| 17 |
from adjutantclient._i18n import _ |
| 18 |
|
| 19 |
verbose = 0 |
| 20 |
|
| 21 |
|
| 22 |
class BaseException(Exception): |
| 23 |
"""An error occurred.""" |
| 24 |
def __init__(self, message=None): |
| 25 |
self.message = message |
| 26 |
|
| 27 |
def __str__(self): |
| 28 |
return self.message or self.__class__.__doc__ |
| 29 |
|
| 30 |
|
| 31 |
class CommandError(BaseException): |
| 32 |
"""Invalid usage of CLI.""" |
| 33 |
|
| 34 |
|
| 35 |
class InvalidEndpoint(BaseException): |
| 36 |
"""The provided endpoint is invalid.""" |
| 37 |
|
| 38 |
|
| 39 |
class CommunicationError(BaseException): |
| 40 |
"""Unable to communicate with server.""" |
| 41 |
|
| 42 |
|
| 43 |
class HTTPException(BaseException): |
| 44 |
"""Base exception for all HTTP-derived exceptions.""" |
| 45 |
code = 'N/A' |
| 46 |
|
| 47 |
def __init__(self, message=None): |
| 48 |
super(HTTPException, self).__init__(message) |
| 49 |
try: |
| 50 |
self.error = jsonutils.loads(message) |
| 51 |
# Adjutant client: mangle the 'errors' return list into |
| 52 |
# standard 'error' format |
| 53 |
if 'errors' in self.error: |
| 54 |
self.error['error'] = { |
| 55 |
"message": ', '.join(self.error['errors']), |
| 56 |
} |
| 57 |
|
| 58 |
if 'error' not in self.error: |
| 59 |
raise KeyError(_('Key "error" does not exist.')) |
| 60 |
except KeyError: |
| 61 |
# NOTE(jianingy): If key 'error' happens not exist, |
| 62 |
# self.message becomes no sense. In this case, we |
| 63 |
# return doc of current exception class instead. |
| 64 |
self.error = {'error': |
| 65 |
{'message': self.__class__.__doc__}} |
| 66 |
except Exception: |
| 67 |
self.error = {'error': |
| 68 |
{'message': self.message or self.__class__.__doc__}} |
| 69 |
|
| 70 |
def __str__(self): |
| 71 |
message = self.error['error'].get('message', 'Internal Error') |
| 72 |
if verbose: |
| 73 |
traceback = self.error['error'].get('traceback', '') |
| 74 |
return (_('ERROR: %(message)s\n%(traceback)s') % |
| 75 |
{'message': message, 'traceback': traceback}) |
| 76 |
else: |
| 77 |
return _('ERROR: %s') % message |
| 78 |
|
| 79 |
|
| 80 |
class HTTPMultipleChoices(HTTPException): |
| 81 |
code = 300 |
| 82 |
|
| 83 |
def __str__(self): |
| 84 |
self.details = _("Requested version of Adjutant API is not" |
| 85 |
"available.") |
| 86 |
return (_("%(name)s (HTTP %(code)s) %(details)s") % |
| 87 |
{ |
| 88 |
'name': self.__class__.__name__, |
| 89 |
'code': self.code, |
| 90 |
'details': self.details |
| 91 |
}) |
| 92 |
|
| 93 |
|
| 94 |
class BadRequest(HTTPException): |
| 95 |
"""DEPRECATED.""" |
| 96 |
code = 400 |
| 97 |
|
| 98 |
|
| 99 |
class HTTPBadRequest(BadRequest): |
| 100 |
pass |
| 101 |
|
| 102 |
|
| 103 |
class Unauthorized(HTTPException): |
| 104 |
"""DEPRECATED.""" |
| 105 |
code = 401 |
| 106 |
|
| 107 |
|
| 108 |
class HTTPUnauthorized(Unauthorized): |
| 109 |
pass |
| 110 |
|
| 111 |
|
| 112 |
class Forbidden(HTTPException): |
| 113 |
"""DEPRECATED.""" |
| 114 |
code = 403 |
| 115 |
|
| 116 |
|
| 117 |
class HTTPForbidden(Forbidden): |
| 118 |
pass |
| 119 |
|
| 120 |
|
| 121 |
class NotFound(HTTPException): |
| 122 |
"""DEPRECATED.""" |
| 123 |
code = 404 |
| 124 |
|
| 125 |
|
| 126 |
class HTTPNotFound(NotFound): |
| 127 |
pass |
| 128 |
|
| 129 |
|
| 130 |
class HTTPMethodNotAllowed(HTTPException): |
| 131 |
code = 405 |
| 132 |
|
| 133 |
|
| 134 |
class Conflict(HTTPException): |
| 135 |
"""DEPRECATED.""" |
| 136 |
code = 409 |
| 137 |
|
| 138 |
|
| 139 |
class HTTPConflict(Conflict): |
| 140 |
pass |
| 141 |
|
| 142 |
|
| 143 |
class OverLimit(HTTPException): |
| 144 |
"""DEPRECATED.""" |
| 145 |
code = 413 |
| 146 |
|
| 147 |
|
| 148 |
class HTTPOverLimit(OverLimit): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class HTTPUnsupported(HTTPException): |
| 153 |
code = 415 |
| 154 |
|
| 155 |
|
| 156 |
class HTTPInternalServerError(HTTPException): |
| 157 |
code = 500 |
| 158 |
|
| 159 |
|
| 160 |
class HTTPNotImplemented(HTTPException): |
| 161 |
code = 501 |
| 162 |
|
| 163 |
|
| 164 |
class HTTPBadGateway(HTTPException): |
| 165 |
code = 502 |
| 166 |
|
| 167 |
|
| 168 |
class ServiceUnavailable(HTTPException): |
| 169 |
"""DEPRECATED.""" |
| 170 |
code = 503 |
| 171 |
|
| 172 |
|
| 173 |
class HTTPServiceUnavailable(ServiceUnavailable): |
| 174 |
pass |
| 175 |
|
| 176 |
|
| 177 |
# NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception |
| 178 |
# classes |
| 179 |
_code_map = {} |
| 180 |
for obj_name in dir(sys.modules[__name__]): |
| 181 |
if obj_name.startswith('HTTP'): |
| 182 |
obj = getattr(sys.modules[__name__], obj_name) |
| 183 |
_code_map[obj.code] = obj |
| 184 |
|
| 185 |
|
| 186 |
def from_response(response): |
| 187 |
"""Return an instance of an HTTPException based on requests response.""" |
| 188 |
cls = _code_map.get(response.status_code, HTTPException) |
| 189 |
try: |
| 190 |
return cls(response.json()) |
| 191 |
except ValueError: |
| 192 |
return cls(response.content) |
| 193 |
|
| 194 |
|
| 195 |
class NoTokenLookupException(Exception): |
| 196 |
"""DEPRECATED.""" |
| 197 |
pass |
| 198 |
|
| 199 |
|
| 200 |
class EndpointNotFound(Exception): |
| 201 |
"""DEPRECATED.""" |
| 202 |
pass |
| 203 |
|
| 204 |
|
| 205 |
class StackFailure(Exception): |
| 206 |
pass |