Anyone that works with DynamoDB is probably familiar with the ConditionalCheckFailed Exception. This error occurs when a condition placed on your api request fails to be satisfied.
Take for example a PutItem request on an Orders table to insert a new record. When using this API, it’s wise to use a ConditionExpression with the value attribute_not_exists. This ensures that if a record is already present with the same key, a ConditionalCheckFailed exception gets thrown. Receiving an exception indicates a problem occurred, but it doesn’t provide you additional context about the item that existed in the table.
But now, a new few feature allows you to receive a copy of the item in the database as it existed when the conditional error is encountered. This is helpful across a broad range of applications including debugging, enhanced logging, and pretty much any other use case that benefits from read after write flows during exception handling.
How ReturnValuesOnConditionCheckFailure Works
How this feature works is pretty simple.
Let’s take a basic PutItem example in Python. This is how our code would look normally without this new feature. We’re doing a simple put_item call and providing our Table name, item, and Condition expression. Notice that if an error does get thrown, all we get back is a simple response code and not much else.