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

Fix exceptions picklability #1044

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions botocore/exceptions.py
Expand Up @@ -24,10 +24,12 @@ class BotoCoreError(Exception):
fmt = 'An unspecified error occurred'

def __init__(self, **kwargs):
msg = self.fmt.format(**kwargs)
Exception.__init__(self, msg)
self.message = self.fmt.format(**kwargs)
self.kwargs = kwargs

def __str__(self):
return self.message


class DataNotFoundError(BotoCoreError):
"""
Expand Down Expand Up @@ -350,13 +352,12 @@ class ClientError(Exception):

def __init__(self, error_response, operation_name):
retry_info = self._get_retry_info(error_response)
msg = self.MSG_TEMPLATE.format(
self.message = self.MSG_TEMPLATE.format(
error_code=error_response['Error'].get('Code', 'Unknown'),
error_message=error_response['Error'].get('Message', 'Unknown'),
operation_name=operation_name,
retry_info=retry_info,
)
super(ClientError, self).__init__(msg)
self.response = error_response
self.operation_name = operation_name

Expand All @@ -370,6 +371,9 @@ def _get_retry_info(self, response):
metadata['RetryAttempts'])
return retry_info

def __str__(self):
return self.message


class UnsupportedTLSVersionWarning(Warning):
"""Warn when an openssl version that uses TLS 1.2 is required"""
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_exceptions.py
Expand Up @@ -11,6 +11,7 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import pickle
from nose.tools import assert_equals

from botocore import exceptions
Expand Down Expand Up @@ -47,6 +48,26 @@ def test_retry_info_added_when_present():
raise AssertionError("retry information not inject into error "
"message: %s" % error_msg)

def test_waiter_error_is_pickleable():
exception = exceptions.WaiterError('object_available', 'timeout', {})
exception2 = pickle.loads(pickle.dumps(exception))

assert_equals(exception.__repr__(), exception2.__repr__())
assert_equals(str(exception), str(exception2))
assert_equals(type(exception), type(exception2))
assert_equals(exception.kwargs, exception2.kwargs)

def test_client_error_is_pickleable():
response = {'Error': {}}

exception = exceptions.ClientError(response, 'blackhole')
exception2 = pickle.loads(pickle.dumps(exception))

assert_equals(exception.__repr__(), exception2.__repr__())
assert_equals(str(exception), str(exception2))
assert_equals(type(exception), type(exception2))
assert_equals(exception.response, exception2.response)
assert_equals(exception.operation_name, exception2.operation_name)

def test_retry_info_not_added_if_retry_attempts_not_present():
response = {
Expand Down