Skip to content

Commit

Permalink
Connection objects now throw an exception when an error is receiv…
Browse files Browse the repository at this point in the history
…ed, rather than silently passing on error data.
  • Loading branch information
toastdriven committed Dec 4, 2013
1 parent 46158fb commit 35bbf42
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
32 changes: 31 additions & 1 deletion boto3/core/connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from boto3.core.constants import DEFAULT_REGION
from boto3.core.constants import NOTHING_PROVIDED
from boto3.core.exceptions import ServerError
from boto3.core.introspection import Introspection
from boto3.utils import six

Expand Down Expand Up @@ -160,6 +161,33 @@ def _build_service_params(self, op_params, **kwargs):

return service_params

def _check_for_errors(self, results):
result_data = results[1]

if 'Errors' in result_data:
errs = result_data['Errors']

if not errs:
# Skip it if the errors are empty.
# For instance, S3 will send this key with nothing in it on a
# successful call.
return

if isinstance(errs, (list, tuple)):
error = errs[0]
elif hasattr(errs, 'items'):
error = errs
else:
error = {
'Message': errs
}

raise ServerError(
code=error.get('Code', 'ConnectionError'),
message=error.get('Message', 'No details available.'),
full_response=result_data
)

def _post_process_results(self, method_name, output, results):
# TODO: Maybe build in an extension mechanism (like
# ``post_process_<op_name>_results``)?
Expand Down Expand Up @@ -277,7 +305,6 @@ def _build_methods(self, details):

def _generate_docstring(self, op_data):
docstring = op_data['docs']
params = []

for param_data in op_data['params']:
param_doc = ":param {0}: {1}\n".format(
Expand Down Expand Up @@ -328,6 +355,9 @@ def _new_method(self, **kwargs):
)
results = op.call(endpoint, **service_params)

# Check for error conditions.
self._check_for_errors(results)

# Post-process results here
post_processed = self._post_process_results(
method_name,
Expand Down
22 changes: 22 additions & 0 deletions boto3/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ class BotoException(Exception):
pass


class ServerError(BotoException):
"""
Thrown when an error is received within a response.
"""
fmt = "[{0}]: {1}"

def __init__(self, code='GeneralError', message='No message',
full_response=None, **kwargs):
self.code = code
self.message = message
self.full_response = full_response

if self.full_response is None:
self.full_response = {}

msg = self.fmt.format(
self.code,
self.message
)
super(ServerError, self).__init__(msg)


class IncorrectImportPath(BotoException):
pass

Expand Down

0 comments on commit 35bbf42

Please sign in to comment.