1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
23
Exception classes - Subclassing allows you to check for specific errors
27
from boto import handler
28
from boto.resultset import ResultSet
31
class BotoClientError(StandardError):
33
General Boto Client error (error accessing AWS)
36
def __init__(self, reason):
37
StandardError.__init__(self)
41
return 'S3Error: %s' % self.reason
44
return 'S3Error: %s' % self.reason
46
class SDBPersistenceError(StandardError):
50
class S3PermissionsError(BotoClientError):
52
Permissions error when accessing a bucket or key on S3.
56
class BotoServerError(StandardError):
58
def __init__(self, status, reason, body=None):
59
StandardError.__init__(self)
62
self.body = body or ''
63
self.request_id = None
64
self.error_code = None
65
self.error_message = None
68
# Attempt to parse the error response. If body isn't present,
69
# then just ignore the error response.
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()
81
def __getattr__(self, name):
83
return self.error_message
85
return self.error_code
89
return '%s: %s %s\n%s' % (self.__class__.__name__,
90
self.status, self.reason, self.body)
93
return '%s: %s %s\n%s' % (self.__class__.__name__,
94
self.status, self.reason, self.body)
96
def startElement(self, name, attrs, connection):
99
def endElement(self, name, value, connection):
100
if name in ('RequestId', 'RequestID'):
101
self.request_id = value
103
self.error_code = value
104
elif name == 'Message':
105
self.error_message = value
106
elif name == 'BoxUsage':
107
self.box_usage = value
110
def _cleanupParsedProperties(self):
111
self.request_id = None
112
self.error_code = None
113
self.error_message = None
114
self.box_usage = None
118
def __init__(self, parent=None):
120
self.instance_id = None
121
self.timestamp = None
125
def startElement(self, name, attrs, connection):
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)
134
setattr(self, name, value)
136
class S3CreateError(BotoServerError):
138
Error creating a bucket or key on S3.
140
def __init__(self, status, reason, body=None):
142
BotoServerError.__init__(self, status, reason, body)
144
def endElement(self, name, value, connection):
145
if name == 'BucketName':
148
return BotoServerError.endElement(self, name, value, connection)
150
class S3CopyError(BotoServerError):
152
Error copying a key on S3.
156
class SQSError(BotoServerError):
158
General Error on Simple Queue Service.
160
def __init__(self, status, reason, body=None):
163
BotoServerError.__init__(self, status, reason, body)
165
def startElement(self, name, attrs, connection):
166
return BotoServerError.startElement(self, name, attrs, connection)
168
def endElement(self, name, value, connection):
174
return BotoServerError.endElement(self, name, value, connection)
176
def _cleanupParsedProperties(self):
177
BotoServerError._cleanupParsedProperties(self)
178
for p in ('detail', 'type'):
179
setattr(self, p, None)
181
class SQSDecodeError(BotoClientError):
183
Error when decoding an SQS message.
185
def __init__(self, reason, message):
186
BotoClientError.__init__(self, reason)
187
self.message = message
190
return 'SQSDecodeError: %s' % self.reason
193
return 'SQSDecodeError: %s' % self.reason
195
class S3ResponseError(BotoServerError):
197
Error in response from S3.
199
def __init__(self, status, reason, body=None):
201
BotoServerError.__init__(self, status, reason, body)
203
def startElement(self, name, attrs, connection):
204
return BotoServerError.startElement(self, name, attrs, connection)
206
def endElement(self, name, value, connection):
207
if name == 'Resource':
208
self.resource = value
210
return BotoServerError.endElement(self, name, value, connection)
212
def _cleanupParsedProperties(self):
213
BotoServerError._cleanupParsedProperties(self)
214
for p in ('resource'):
215
setattr(self, p, None)
217
class EC2ResponseError(BotoServerError):
219
Error in response from EC2.
222
def __init__(self, status, reason, body=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 ]
229
self.error_code, self.error_message = self.errors[0]
231
def startElement(self, name, attrs, connection):
233
self._errorResultSet = ResultSet([('Error', _EC2Error)])
234
return self._errorResultSet
238
def endElement(self, name, value, connection):
239
if name == 'RequestID':
240
self.request_id = value
242
return None # don't call subclass here
244
def _cleanupParsedProperties(self):
245
BotoServerError._cleanupParsedProperties(self)
246
self._errorResultSet = []
248
setattr(self, p, None)
250
class EmrResponseError(BotoServerError):
252
Error in response from EMR
258
def __init__(self, connection=None):
259
self.connection = connection
260
self.error_code = None
261
self.error_message = None
263
def startElement(self, name, attrs, connection):
266
def endElement(self, name, value, connection):
268
self.error_code = value
269
elif name == 'Message':
270
self.error_message = value
274
class SDBResponseError(BotoServerError):
276
Error in respones from SDB.
280
class AWSConnectionError(BotoClientError):
282
General error connecting to Amazon Web Services.
286
class S3DataError(BotoClientError):
288
Error receiving data from S3.
292
class FPSResponseError(BotoServerError):