Skip to content

Commit

Permalink
Raise appropriate exceptions for condition failures (pynamodb#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
koreno committed Oct 25, 2023
1 parent c1d6222 commit 93ef963
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pynamodb/connection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,10 @@ def update_item(
try:
return self.dispatch(UPDATE_ITEM, operation_kwargs, settings)
except BOTOCORE_EXCEPTIONS as e:
raise UpdateError("Failed to update item: {}".format(e), e)
exc = UpdateError("Failed to update item: {}".format(e), e)
if exc.cause_response_code == "ConditionalCheckFailedException":
exc.__class__ = ConditionFailedUpdate
raise exc

def put_item(
self,
Expand Down Expand Up @@ -1079,7 +1082,10 @@ def put_item(
try:
return self.dispatch(PUT_ITEM, operation_kwargs, settings)
except BOTOCORE_EXCEPTIONS as e:
raise PutError("Failed to put item: {}".format(e), e)
exc = PutError("Failed to put item: {}".format(e), e)
if exc.cause_response_code == "ConditionalCheckFailedException":
exc.__class__ = ConditionFailedPut
raise exc

def _get_transact_operation_kwargs(
self,
Expand Down
14 changes: 14 additions & 0 deletions pynamodb/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,27 @@ class PutError(PynamoDBConnectionError):
msg = "Error putting item"


class ConditionFailedPut(PutError):
"""
Raised when an item fails to be created due to condition
"""
msg = "Condition failed while putting item"


class UpdateError(PynamoDBConnectionError):
"""
Raised when an item fails to be updated
"""
msg = "Error updating item"


class ConditionFailedUpdate(UpdateError):
"""
Raised when an item fails to be updated due to condition
"""
msg = "Condition failed while updating item"


class GetError(PynamoDBConnectionError):
"""
Raised when an item fails to be retrieved
Expand Down

0 comments on commit 93ef963

Please sign in to comment.