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 ClientError getters for common attributes #3251

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions botocore/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,31 @@ def __reduce__(self):
# module. So at the very least return a ClientError back.
return ClientError, (self.response, self.operation_name)

@property
def code(self):
"""The error code returned by the AWS service."""
return self.response.get('Error', {}).get('Code', 'Unknown')

@property
def message(self):
"""The error message returned by the AWS service."""
return self.response.get('Error', {}).get('Message', 'Unknown')

@property
def request_id(self):
"""The request ID returned by the AWS service."""
return self.response.get('ResponseMetadata').get('RequestId')

@property
def http_status_code(self):
"""The HTTP status code returned by the AWS service."""
return self.response.get('ResponseMetadata', {}).get('HTTPStatusCode')

@property
def http_headers(self):
"""The HTTP headers returned by the AWS service."""
return self.response.get('ResponseMetadata', {}).get('HTTPHeaders', {})


class EventStreamError(ClientError):
pass
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ def test_client_error_can_handle_missing_code_or_message():
response = {'Error': {}}
expect = 'An error occurred (Unknown) when calling the blackhole operation: Unknown'
assert str(exceptions.ClientError(response, 'blackhole')) == expect
assert exceptions.ClientError.code == 'Unknown'
assert exceptions.ClientError.message == 'Unknown'


def test_client_error_can_handle_missing_response_metadata():
response = {'Error': {}}
assert (
exceptions.ClientError(response, 'blackhole').http_status_code is None
)
assert exceptions.ClientError(response, 'blackhole').request_id is None
assert exceptions.ClientError(response, 'blackhole').http_headers == {}


def test_client_error_has_operation_name_set():
Expand Down