I am using golang sdk https://pkg.go.dev/github.com/aws/[email protected]/ to debug conditional check errors and find information about why the failure occured in sIngle write operations, but I can only see the error Message_: "The conditional request failed". No other information regarding specific reason is provided when using the parameter ReturnValuesOnConditionCheckFailure: ALL_OLD in UpdateItemInput. In case of TransactWriteItems I can see specific reason of conditional check failure when using the same parameter. How can I obtain those details for single write operations as well? Reference: https://aws.amazon.com/about-aws/whats-new/2023/06/amazon-dynamodb-cost-failed-conditional-writes The syntax I am using:

input := &dynamodb.UpdateItemInput{
        TableName:                           aws.String("DummyTable"),
        Key:                                 keyAttr,
        ExpressionAttributeValues:           updateExpr.Values(),
        ExpressionAttributeNames:            updateExpr.Names(),
        ConditionExpression:                 updateExpr.Condition(),
        ReturnValues:                        aws.String(dynamodb.ReturnValueAllOld),
        UpdateExpression:                    updateExpr.Update(),
        ReturnValuesOnConditionCheckFailure: aws.String(dynamodb.ReturnValuesOnConditionCheckFailureAllOld),
}
output, err := dl.ddbI.UpdateItem(input)
2

There are 2 answers

6
Leeroy Hannigan On BEST ANSWER

The item should be within the error component usually in error.response.Item.

For example in python:

except botocore.exceptions.ClientError as error:
    if error.response["Error"]["Code"] == "ConditionalCheckFailedException":
        print("The conditional expression is not met")
        current_value = error.response.get("Item")

Note: if you're using DynamoDB Local this feature does not yet exist

0
Marcos Felipe On

As of Dec 18 2023, local dynamo supports it (versions 1.25.0 and 2.2.0 onwards): https://aws.amazon.com/about-aws/whats-new/2023/12/amazon-dynamodb-local-two-api-features/

In go you could do something like this. Remember that the data won't come at *dynamodb.UpdateItemOutput, but at the error return var:

_, err = r.client.UpdateItem(context, updateItemInput)
if err != nil {
    var condErr *dynamotypes.ConditionalCheckFailedException
    if errors.As(err, &condErr) {
        if len(condErr.Item) > 0 {
          ...
        }
    }
}