NIKHIL C M
NIKHIL C M

Reputation: 4236

Way to distinguish dynamodb TransactionCanceledException

I have an api with transaction which uses DynamoDB as the database. I will be getting two kind of exceptions. One is due to ConditionalCheckFailed exception when the condition is not met. The error object looks like this:

{
    "message": "Transaction cancelled, please refer cancellation reasons for specific reasons [None, ConditionalCheckFailed]",
    "code": "TransactionCanceledException",
    "time": "2020-01-22T05:46:32.756Z",
    "requestId": "UG14A08TDB6Q5CADF0NH9JQAB3VV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 32.837614849025734
}

Another scenario is when I do the load testing on the same api, I will be getting TransactionConflict exception.

{
    "message": "Transaction cancelled, please refer cancellation reasons for specific reasons [None, TransactionConflict]",
    "code": "TransactionCanceledException",
    "time": "2020-01-22T05:54:40.940Z",
    "requestId": "87MHRV37F3G3EUUF629AKICARBVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 31.786748424710908
}

I want to retry the transaction on error due to TransactionConflict exception.But I am not sure how to conditionally retry the transaction with this error information. Since the message part is the only key which tells me the exact reason of the error and not the error code itself.

I don't think comparing the message is a good way to conditionally handle error.

Upvotes: 5

Views: 9821

Answers (2)

Michael - sqlbot
Michael - sqlbot

Reputation: 179154

I don't think comparing the message is a good way to conditionally handle error.

I agree completely, but:

  • AWS is very good about not changing APIs, so you are probably okay with this approach, and
  • they don't appear to have given you any alternative.

A safe approach, I would suggest, is to string-match the message, but also to throw your own exception if you get a TransactionCanceledException with a message that doesn't match any expected string -- don't simply test whether it is equal/not equal to one possible string, but instead test it for all known/relevant values and if it matches no known pattern, halt and catch fire. That eliminates the risk of unexpected changes to the text of the error silently causing your code to misbehave.

Upvotes: 5

dashuser
dashuser

Reputation: 181

As far as I know all 400 errors from DynamoDB are NON retry-able. It means that there is a issue with the way the API request is received and its not right and hence the 400 Error.

So, in your case, I would suggest fixing the issue and then adding a retry when you receive a retry-able exception. For more reference : https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html

Only 500 errors are retry-able.

Upvotes: 0

Related Questions