~ubuntu-branches/ubuntu/wily/python-ceilometerclient/wily

« back to all changes in this revision

Viewing changes to ceilometerclient/exc.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-03-01 12:47:39 UTC
  • Revision ID: package-import@ubuntu.com-20130301124739-0in66psacnqpksch
Tags: upstream-0.1~dde86a3
ImportĀ upstreamĀ versionĀ 0.1~dde86a3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
16
class BaseException(Exception):
 
17
    """An error occurred."""
 
18
    def __init__(self, message=None):
 
19
        self.message = message
 
20
 
 
21
    def __str__(self):
 
22
        return self.message or self.__class__.__doc__
 
23
 
 
24
 
 
25
class CommandError(BaseException):
 
26
    """Invalid usage of CLI"""
 
27
 
 
28
 
 
29
class InvalidEndpoint(BaseException):
 
30
    """The provided endpoint is invalid."""
 
31
 
 
32
 
 
33
class CommunicationError(BaseException):
 
34
    """Unable to communicate with server."""
 
35
 
 
36
 
 
37
class ClientException(Exception):
 
38
    """DEPRECATED"""
 
39
 
 
40
 
 
41
class HTTPException(ClientException):
 
42
    """Base exception for all HTTP-derived exceptions"""
 
43
    code = 'N/A'
 
44
 
 
45
    def __init__(self, details=None):
 
46
        self.details = details
 
47
 
 
48
    def __str__(self):
 
49
        return "%s (HTTP %s)" % (self.__class__.__name__, self.code)
 
50
 
 
51
 
 
52
class HTTPMultipleChoices(HTTPException):
 
53
    code = 300
 
54
 
 
55
    def __str__(self):
 
56
        self.details = ("Requested version of OpenStack Images API is not"
 
57
                        "available.")
 
58
        return "%s (HTTP %s) %s" % (self.__class__.__name__, self.code,
 
59
                                    self.details)
 
60
 
 
61
 
 
62
class BadRequest(HTTPException):
 
63
    """DEPRECATED"""
 
64
    code = 400
 
65
 
 
66
 
 
67
class HTTPBadRequest(BadRequest):
 
68
    pass
 
69
 
 
70
 
 
71
class Unauthorized(HTTPException):
 
72
    """DEPRECATED"""
 
73
    code = 401
 
74
 
 
75
 
 
76
class HTTPUnauthorized(Unauthorized):
 
77
    pass
 
78
 
 
79
 
 
80
class Forbidden(HTTPException):
 
81
    """DEPRECATED"""
 
82
    code = 403
 
83
 
 
84
 
 
85
class HTTPForbidden(Forbidden):
 
86
    pass
 
87
 
 
88
 
 
89
class NotFound(HTTPException):
 
90
    """DEPRECATED"""
 
91
    code = 404
 
92
 
 
93
 
 
94
class HTTPNotFound(NotFound):
 
95
    pass
 
96
 
 
97
 
 
98
class HTTPMethodNotAllowed(HTTPException):
 
99
    code = 405
 
100
 
 
101
 
 
102
class Conflict(HTTPException):
 
103
    """DEPRECATED"""
 
104
    code = 409
 
105
 
 
106
 
 
107
class HTTPConflict(Conflict):
 
108
    pass
 
109
 
 
110
 
 
111
class OverLimit(HTTPException):
 
112
    """DEPRECATED"""
 
113
    code = 413
 
114
 
 
115
 
 
116
class HTTPOverLimit(OverLimit):
 
117
    pass
 
118
 
 
119
 
 
120
class HTTPInternalServerError(HTTPException):
 
121
    code = 500
 
122
 
 
123
 
 
124
class HTTPNotImplemented(HTTPException):
 
125
    code = 501
 
126
 
 
127
 
 
128
class HTTPBadGateway(HTTPException):
 
129
    code = 502
 
130
 
 
131
 
 
132
class ServiceUnavailable(HTTPException):
 
133
    """DEPRECATED"""
 
134
    code = 503
 
135
 
 
136
 
 
137
class HTTPServiceUnavailable(ServiceUnavailable):
 
138
    pass
 
139
 
 
140
 
 
141
#NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
 
142
# classes
 
143
_code_map = {}
 
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
 
148
 
 
149
 
 
150
def from_response(response):
 
151
    """Return an instance of an HTTPException based on httplib response."""
 
152
    cls = _code_map.get(response.status, HTTPException)
 
153
    return cls()
 
154
 
 
155
 
 
156
class NoTokenLookupException(Exception):
 
157
    """DEPRECATED"""
 
158
    pass
 
159
 
 
160
 
 
161
class EndpointNotFound(Exception):
 
162
    """DEPRECATED"""
 
163
    pass