question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle ConditionalCheckFailedException

See original GitHub issue

My code looks like below:

try:
    cust_table.put_item(Item=company,
                    ConditionExpression=Attr('version').eq(previous_version))
except botocore.errorfactory.ConditionalCheckFailedException as ex:
  print(ex)

I’m getting this error

# botocore.errorfactory.ConditionalCheckFailedException: An error occurred (ConditionalCheckFailedException) when calling the PutItem operation: The conditional request failed

# During handling of the above exception, another exception occurred:

# Traceback (most recent call last):
#   File "/home/ganesh/code/vantage-trials-site-reassignment-service/createUser/src/test.py", line 45, in <module>
#     except botocore.errorfactory.ConditionalCheckFailedException as ex:
# AttributeError: module 'botocore.errorfactory' has no attribute 'ConditionalCheckFailedException'

What exception to catch?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

43reactions
swetashrecommented, Jan 16, 2020

@ghsatpute - You can achieve the same using the errorfactory which will create an error class based on the codes returned. This was added here

import boto3

res = boto3.resource('dynamodb')
table = res.Table('mytable') 

try: 
    conditionalUpdateResponse = table.put_item(Item={'test':'testing20'},ConditionExpression='attribute_exists(test)')
except res.meta.client.exceptions.ConditionalCheckFailedException as e: 
    print(e)

Hope this helps. We don’t have very good documentation about how to use errorfactory.

20reactions
ghsatputecommented, Dec 18, 2019

So as per my experience with programming. We catch exceptions which have been raised or their parent. For example, if a code raises FileNotFoundException we catch FileNotFoundException or it’s parent IOException if you don’t want to differentiate between types of exception.

Here, the code is raising ConditionalCheckFailedException, I should be able to catch the same exception. Here we are catching some other exception and writing logic inside to check whether it’s a part of the exception.

Obviously, this functionally works, but the working code is not everything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling with DynamoDB - AWS Documentation
Error messages and codes · AccessDeniedException. Message: Access denied. · ConditionalCheckFailedException · IncompleteSignatureException.
Read more >
[Solved] DynamoDB ConditionalCheckFailedException
Solution: Either remove ConditionExpression from your DynamoDB code or change parameters and data so the condition can be satisfied.
Read more >
javascript - I am facing ConditionalCheckFailedException ...
it just means that no such key exists in database, so the conditional request is failing. – itaintme. Jul 16, 2018 at 19:03....
Read more >
r/aws - Reddit
2 questions: Do you see any glaring problems handling "ConditionalCheckFailedException" errors this way? Is there a better way?
Read more >
DynamoDB — botocore 1.29.25 documentation - Amazon AWS
For more information, see Batch Operations and Error Handling in the Amazon DynamoDB Developer Guide . ... ConditionalCheckFailedException; DynamoDB.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found