~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/boto/boto/exception.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
"""
 
23
Exception classes - Subclassing allows you to check for specific errors
 
24
"""
 
25
import base64
 
26
import xml.sax
 
27
from boto import handler
 
28
from boto.resultset import ResultSet
 
29
 
 
30
 
 
31
class BotoClientError(StandardError):
 
32
    """
 
33
    General Boto Client error (error accessing AWS)
 
34
    """
 
35
    
 
36
    def __init__(self, reason):
 
37
        StandardError.__init__(self)
 
38
        self.reason = reason
 
39
 
 
40
    def __repr__(self):
 
41
        return 'S3Error: %s' % self.reason
 
42
 
 
43
    def __str__(self):
 
44
        return 'S3Error: %s' % self.reason
 
45
 
 
46
class SDBPersistenceError(StandardError):
 
47
 
 
48
    pass
 
49
 
 
50
class S3PermissionsError(BotoClientError):
 
51
    """
 
52
    Permissions error when accessing a bucket or key on S3.
 
53
    """
 
54
    pass
 
55
    
 
56
class BotoServerError(StandardError):
 
57
    
 
58
    def __init__(self, status, reason, body=None):
 
59
        StandardError.__init__(self)
 
60
        self.status = status
 
61
        self.reason = reason
 
62
        self.body = body or ''
 
63
        self.request_id = None
 
64
        self.error_code = None
 
65
        self.error_message = None
 
66
        self.box_usage = None
 
67
 
 
68
        # Attempt to parse the error response. If body isn't present,
 
69
        # then just ignore the error response.
 
70
        if self.body:
 
71
            try:
 
72
                h = handler.XmlHandler(self, self)
 
73
                xml.sax.parseString(self.body, h)
 
74
            except xml.sax.SAXParseException, pe:
 
75
                # Go ahead and clean up anything that may have
 
76
                # managed to get into the error data so we
 
77
                # don't get partial garbage.
 
78
                print "Warning: failed to parse error message from AWS: %s" % pe
 
79
                self._cleanupParsedProperties()
 
80
 
 
81
    def __getattr__(self, name):
 
82
        if name == 'message':
 
83
            return self.error_message
 
84
        if name == 'code':
 
85
            return self.error_code
 
86
        raise AttributeError
 
87
 
 
88
    def __repr__(self):
 
89
        return '%s: %s %s\n%s' % (self.__class__.__name__,
 
90
                                  self.status, self.reason, self.body)
 
91
 
 
92
    def __str__(self):
 
93
        return '%s: %s %s\n%s' % (self.__class__.__name__,
 
94
                                  self.status, self.reason, self.body)
 
95
 
 
96
    def startElement(self, name, attrs, connection):
 
97
        pass
 
98
 
 
99
    def endElement(self, name, value, connection):
 
100
        if name in ('RequestId', 'RequestID'):
 
101
            self.request_id = value
 
102
        elif name == 'Code':
 
103
            self.error_code = value
 
104
        elif name == 'Message':
 
105
            self.error_message = value
 
106
        elif name == 'BoxUsage':
 
107
            self.box_usage = value
 
108
        return None
 
109
 
 
110
    def _cleanupParsedProperties(self):
 
111
        self.request_id = None
 
112
        self.error_code = None
 
113
        self.error_message = None
 
114
        self.box_usage = None
 
115
 
 
116
class ConsoleOutput:
 
117
 
 
118
    def __init__(self, parent=None):
 
119
        self.parent = parent
 
120
        self.instance_id = None
 
121
        self.timestamp = None
 
122
        self.comment = None
 
123
        self.output = None
 
124
 
 
125
    def startElement(self, name, attrs, connection):
 
126
        return None
 
127
 
 
128
    def endElement(self, name, value, connection):
 
129
        if name == 'instanceId':
 
130
            self.instance_id = value
 
131
        elif name == 'output':
 
132
            self.output = base64.b64decode(value)
 
133
        else:
 
134
            setattr(self, name, value)
 
135
 
 
136
class S3CreateError(BotoServerError):
 
137
    """
 
138
    Error creating a bucket or key on S3.
 
139
    """
 
140
    def __init__(self, status, reason, body=None):
 
141
        self.bucket = None
 
142
        BotoServerError.__init__(self, status, reason, body)
 
143
 
 
144
    def endElement(self, name, value, connection):
 
145
        if name == 'BucketName':
 
146
            self.bucket = value
 
147
        else:
 
148
            return BotoServerError.endElement(self, name, value, connection)
 
149
 
 
150
class S3CopyError(BotoServerError):
 
151
    """
 
152
    Error copying a key on S3.
 
153
    """
 
154
    pass
 
155
 
 
156
class SQSError(BotoServerError):
 
157
    """
 
158
    General Error on Simple Queue Service.
 
159
    """
 
160
    def __init__(self, status, reason, body=None):
 
161
        self.detail = None
 
162
        self.type = None
 
163
        BotoServerError.__init__(self, status, reason, body)
 
164
 
 
165
    def startElement(self, name, attrs, connection):
 
166
        return BotoServerError.startElement(self, name, attrs, connection)
 
167
 
 
168
    def endElement(self, name, value, connection):
 
169
        if name == 'Detail':
 
170
            self.detail = value
 
171
        elif name == 'Type':
 
172
            self.type = value
 
173
        else:
 
174
            return BotoServerError.endElement(self, name, value, connection)
 
175
 
 
176
    def _cleanupParsedProperties(self):
 
177
        BotoServerError._cleanupParsedProperties(self)
 
178
        for p in ('detail', 'type'):
 
179
            setattr(self, p, None)
 
180
 
 
181
class SQSDecodeError(BotoClientError):
 
182
    """
 
183
    Error when decoding an SQS message.
 
184
    """
 
185
    def __init__(self, reason, message):
 
186
        BotoClientError.__init__(self, reason)
 
187
        self.message = message
 
188
 
 
189
    def __repr__(self):
 
190
        return 'SQSDecodeError: %s' % self.reason
 
191
 
 
192
    def __str__(self):
 
193
        return 'SQSDecodeError: %s' % self.reason
 
194
    
 
195
class S3ResponseError(BotoServerError):
 
196
    """
 
197
    Error in response from S3.
 
198
    """
 
199
    def __init__(self, status, reason, body=None):
 
200
        self.resource = None
 
201
        BotoServerError.__init__(self, status, reason, body)
 
202
 
 
203
    def startElement(self, name, attrs, connection):
 
204
        return BotoServerError.startElement(self, name, attrs, connection)
 
205
 
 
206
    def endElement(self, name, value, connection):
 
207
        if name == 'Resource':
 
208
            self.resource = value
 
209
        else:
 
210
            return BotoServerError.endElement(self, name, value, connection)
 
211
 
 
212
    def _cleanupParsedProperties(self):
 
213
        BotoServerError._cleanupParsedProperties(self)
 
214
        for p in ('resource'):
 
215
            setattr(self, p, None)
 
216
 
 
217
class EC2ResponseError(BotoServerError):
 
218
    """
 
219
    Error in response from EC2.
 
220
    """
 
221
 
 
222
    def __init__(self, status, reason, body=None):
 
223
        self.errors = None
 
224
        self._errorResultSet = []
 
225
        BotoServerError.__init__(self, status, reason, body)
 
226
        self.errors = [ (e.error_code, e.error_message) \
 
227
                for e in self._errorResultSet ]
 
228
        if len(self.errors):
 
229
            self.error_code, self.error_message = self.errors[0]
 
230
 
 
231
    def startElement(self, name, attrs, connection):
 
232
        if name == 'Errors':
 
233
            self._errorResultSet = ResultSet([('Error', _EC2Error)])
 
234
            return self._errorResultSet
 
235
        else:
 
236
            return None
 
237
 
 
238
    def endElement(self, name, value, connection):
 
239
        if name == 'RequestID':
 
240
            self.request_id = value
 
241
        else:
 
242
            return None # don't call subclass here
 
243
 
 
244
    def _cleanupParsedProperties(self):
 
245
        BotoServerError._cleanupParsedProperties(self)
 
246
        self._errorResultSet = []
 
247
        for p in ('errors'):
 
248
            setattr(self, p, None)
 
249
 
 
250
class EmrResponseError(BotoServerError):
 
251
    """
 
252
    Error in response from EMR
 
253
    """
 
254
    pass
 
255
 
 
256
class _EC2Error:
 
257
 
 
258
    def __init__(self, connection=None):
 
259
        self.connection = connection
 
260
        self.error_code = None
 
261
        self.error_message = None
 
262
 
 
263
    def startElement(self, name, attrs, connection):
 
264
        return None
 
265
 
 
266
    def endElement(self, name, value, connection):
 
267
        if name == 'Code':
 
268
            self.error_code = value
 
269
        elif name == 'Message':
 
270
            self.error_message = value
 
271
        else:
 
272
            return None
 
273
 
 
274
class SDBResponseError(BotoServerError):
 
275
    """
 
276
    Error in respones from SDB.
 
277
    """
 
278
    pass
 
279
 
 
280
class AWSConnectionError(BotoClientError):
 
281
    """
 
282
    General error connecting to Amazon Web Services.
 
283
    """
 
284
    pass
 
285
 
 
286
class S3DataError(BotoClientError):
 
287
    """
 
288
    Error receiving data from S3.
 
289
    """ 
 
290
    pass
 
291
 
 
292
class FPSResponseError(BotoServerError):
 
293
    pass