Skip to content

Commit

Permalink
helper functions to get error code and message from response (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallie authored and ikonst committed Jul 8, 2019
1 parent ae9dc08 commit 7ea92e6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ cover/

# PyCharm
.idea/
build/

# Ignore Cache
.cache/
8 changes: 8 additions & 0 deletions pynamodb/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def __init__(self, msg=None, cause=None):
self.cause = cause
super(PynamoDBException, self).__init__(self.msg)

@property
def cause_response_code(self):
return getattr(self.cause, 'response', {}).get('Error', {}).get('Code')

@property
def cause_response_message(self):
return getattr(self.cause, 'response', {}).get('Error', {}).get('Message')


class PynamoDBConnectionError(PynamoDBException):
"""
Expand Down
4 changes: 4 additions & 0 deletions pynamodb/exceptions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class PynamoDBException(Exception):
msg: str
cause: Any
def __init__(self, msg: Optional[Text] = ..., cause: Optional[Exception] = ...) -> None: ...
@property
def cause_response_code(self) -> Optional[str]: ...
@property
def cause_response_message(self) -> Optional[str]: ...

class PynamoDBConnectionError(PynamoDBException): ...
class DeleteError(PynamoDBConnectionError): ...
Expand Down
42 changes: 42 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from botocore.exceptions import ClientError

from pynamodb.exceptions import PutError


def test_get_cause_response_code():
error = PutError(
cause=ClientError(
error_response={
'Error': {
'Code': 'hello'
}
},
operation_name='test'
)
)
assert error.cause_response_code == 'hello'


def test_get_cause_response_code__no_code():
error = PutError()
assert error.cause_response_code is None


def test_get_cause_response_message():
error = PutError(
cause=ClientError(
error_response={
'Error': {
'Message': 'hiya'
}
},
operation_name='test'
)
)
assert error.cause_response_message == 'hiya'


def test_get_cause_response_message__no_message():
error = PutError()
assert error.cause_response_message is None

0 comments on commit 7ea92e6

Please sign in to comment.