|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 17 | + |
| 18 | +""" |
| 19 | +Exception definitions. |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +class CommandError(Exception): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class AuthorizationFailure(Exception): |
| 28 | + pass |
| 29 | + |
| 30 | + |
| 31 | +class NoTokenLookupException(Exception): |
| 32 | + """This form of authentication does not support looking up |
| 33 | + endpoints from an existing token.""" |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +class EndpointNotFound(Exception): |
| 38 | + """Could not find Service or Region in Service Catalog.""" |
| 39 | + pass |
| 40 | + |
| 41 | + |
| 42 | +class ClientException(Exception): |
| 43 | + """ |
| 44 | + The base exception class for all exceptions this library raises. |
| 45 | + """ |
| 46 | + def __init__(self, code, message=None, details=None): |
| 47 | + self.code = code |
| 48 | + self.message = message or self.__class__.message |
| 49 | + self.details = details |
| 50 | + |
| 51 | + def __str__(self): |
| 52 | + return "%s (HTTP %s)" % (self.message, self.code) |
| 53 | + |
| 54 | + |
| 55 | +class BadRequest(ClientException): |
| 56 | + """ |
| 57 | + HTTP 400 - Bad request: you sent some malformed data. |
| 58 | + """ |
| 59 | + http_status = 400 |
| 60 | + message = "Bad request" |
| 61 | + |
| 62 | + |
| 63 | +class Unauthorized(ClientException): |
| 64 | + """ |
| 65 | + HTTP 401 - Unauthorized: bad credentials. |
| 66 | + """ |
| 67 | + http_status = 401 |
| 68 | + message = "Unauthorized" |
| 69 | + |
| 70 | + |
| 71 | +class Forbidden(ClientException): |
| 72 | + """ |
| 73 | + HTTP 403 - Forbidden: your credentials don't give you access to this |
| 74 | + resource. |
| 75 | + """ |
| 76 | + http_status = 403 |
| 77 | + message = "Forbidden" |
| 78 | + |
| 79 | + |
| 80 | +class NotFound(ClientException): |
| 81 | + """ |
| 82 | + HTTP 404 - Not found |
| 83 | + """ |
| 84 | + http_status = 404 |
| 85 | + message = "Not found" |
| 86 | + |
| 87 | + |
| 88 | +class Conflict(ClientException): |
| 89 | + """ |
| 90 | + HTTP 409 - Conflict |
| 91 | + """ |
| 92 | + http_status = 409 |
| 93 | + message = "Conflict" |
| 94 | + |
| 95 | + |
| 96 | +class OverLimit(ClientException): |
| 97 | + """ |
| 98 | + HTTP 413 - Over limit: you're over the API limits for this time period. |
| 99 | + """ |
| 100 | + http_status = 413 |
| 101 | + message = "Over limit" |
| 102 | + |
| 103 | + |
| 104 | +# NotImplemented is a python keyword. |
| 105 | +class HTTPNotImplemented(ClientException): |
| 106 | + """ |
| 107 | + HTTP 501 - Not Implemented: the server does not support this operation. |
| 108 | + """ |
| 109 | + http_status = 501 |
| 110 | + message = "Not Implemented" |
| 111 | + |
| 112 | + |
| 113 | +# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__() |
| 114 | +# so we can do this: |
| 115 | +# _code_map = dict((c.http_status, c) |
| 116 | +# for c in ClientException.__subclasses__()) |
| 117 | +# |
| 118 | +# Instead, we have to hardcode it: |
| 119 | +_code_map = dict((c.http_status, c) for c in [BadRequest, Unauthorized, |
| 120 | + Forbidden, NotFound, OverLimit, HTTPNotImplemented]) |
| 121 | + |
| 122 | + |
| 123 | +def from_response(response, body): |
| 124 | + """ |
| 125 | + Return an instance of an ClientException or subclass |
| 126 | + based on an httplib2 response. |
| 127 | +
|
| 128 | + Usage:: |
| 129 | +
|
| 130 | + resp, body = http.request(...) |
| 131 | + if resp.status != 200: |
| 132 | + raise exception_from_response(resp, body) |
| 133 | + """ |
| 134 | + cls = _code_map.get(response.status, ClientException) |
| 135 | + if body: |
| 136 | + if hasattr(body, 'keys'): |
| 137 | + error = body[body.keys()[0]] |
| 138 | + message = error.get('message', None) |
| 139 | + details = error.get('details', None) |
| 140 | + else: |
| 141 | + # If we didn't get back a properly formed error message we |
| 142 | + # probably couldn't communicate with Keystone at all. |
| 143 | + message = "Unable to communicate with image service: %s." % body |
| 144 | + details = None |
| 145 | + return cls(code=response.status, message=message, details=details) |
| 146 | + else: |
| 147 | + return cls(code=response.status) |
0 commit comments