Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compat for legacy operation methods on waiters #442

Merged
merged 1 commit into from
Jan 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions botocore/waiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,28 @@ def __init__(self, operation_object, endpoint):
self._endpoint = endpoint

def __call__(self, **kwargs):
http, parsed = self._operation_object.call(
self._endpoint, **kwargs)
try:
http, parsed = self._operation_object.call(
self._endpoint, **kwargs)
except Exception as e:
# In theory, a handler can raise an type of exception.
# We're going to make a best effort attempt to handle
# the ClientError attributes, but not require that
# the exception is an instance of ClientError.
if self._looks_like_client_error(e):
return {
'Error': {
'Code': e.error_code,
'Message': e.error_message,
}
}
else:
raise
return parsed

def _looks_like_client_error(self, e):
return hasattr(e, 'error_code') and hasattr(e, 'error_message')


class WaiterModel(object):
SUPPORTED_VERSION = 2
Expand Down Expand Up @@ -317,6 +335,8 @@ def wait(self, **kwargs):
raise WaiterError(name=self.name,
reason='Unexpected error encountered.')
if current_state == 'success':
logger.debug("Waiting complete, waiter matched the "
"success state.")
return
if current_state == 'failure':
raise WaiterError(
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,28 @@ def test_legacy_op_method_makes_call(self):
operation_object.call.assert_called_with(
endpoint, Foo='a', Bar='b')

def test_legacy_method_handles_exceptions(self):
operation_object = mock.Mock()
exception = Exception()
exception.error_message = 'Foo'
exception.error_code = 'MyCode'
operation_object.call.side_effect = exception
endpoint = mock.Mock()
op = LegacyOperationMethod(operation_object, endpoint)
response = op(Foo='a', Bar='b')
self.assertEqual(response,
{'Error': {'Code': 'MyCode', 'Message': 'Foo'}})

def test_legacy_method_with_unknown_exception(self):
operation_object = mock.Mock()
# A generic exception missing the error_message and error_code
# attrs will just be reraised.
operation_object.call.side_effect = ValueError
endpoint = mock.Mock()
op = LegacyOperationMethod(operation_object, endpoint)
with self.assertRaises(ValueError):
op(Foo='a', Bar='b')


class ServiceWaiterFunctionalTest(BaseEnvVar):
"""
Expand Down