soto-project / soto

Swift SDK for AWS that works on Linux, macOS and iOS
https://soto.codes
Apache License 2.0
876 stars 83 forks source link

DynamoDB Transaction Conflict Expected Behaviour #532

Closed khinkson closed 2 years ago

khinkson commented 2 years ago

Describe the bug Using DynamoDB transactWriteItems to trigger a transaction conflict with one or more of the items being written, I expected the thrown error to be a DynamoDBErrorType (transactionConflictException specifically or even transactionCanceledException). However the thrown error is a SotoCore.AWSRawError? Is this the expected behaviour.

To Reproduce Steps to reproduce the behavior:

  1. Call transactWriteItems with one or more items that will trigger a transaction conflict
  2. Try to catch the thrown error as a DynamoDBErrorType
  3. ...

Expected behavior The thrown error should be a DynamoDBErrorType of type transactionConflictException.

Setup (please complete the following information):

Additional context Taking a look at the data returned from the DynamoDB api for transactionConflictException or transactionCanceledException it looks like those data types might be fairly variable? I'm not sure if that is part of the issue.

adam-fowler commented 2 years ago

If you run with the logging middleware what is the output. To run with logging middleware setup your dynamodb object as follows

let dynamoDB = DynamoDB(client: ...)
    .with(middlewares: [AWSLoggingMiddleware()])
khinkson commented 2 years ago

Request: TransactWriteItems POST https://dynamodb.us-east-2.amazonaws.com/ Headers: [ x-amz-target : DynamoDB_20120810.TransactWriteItems user-agent : Soto/5.0 content-type : application/x-amz-json-1.0 ] Body: {"TransactItems":[{"Put":{"Item":{"created":{"S":"1633092001413.284912"},"pk":{"S":"75830cde-cdd9-4e8d-b4dd-f7030ebef479-1|1|PASSWORD|1d7d8961-de9b-4437-80ef-342b8295769a-1|USER"},"sk":{"S":"a4d25b32-95b7-456f-8f1d-77a7891c3b76-1"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"created":{"S":"1633092001413.251953"},"pk":{"S":"75830cde-cdd9-4e8d-b4dd-f7030ebef479-1|1|EMAIL|26b4fb37-36f1-44de-a707-d9e42b31f18e-1|USER"},"sk":{"S":"a4d25b32-95b7-456f-8f1d-77a7891c3b76-1"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"secret":{"S":"l0bfPoRhpdz\/lwTJZA07IGhglasi6ocO+gbyMC34M8Z+ehSWcdGZ8AjvE\/+RSIXJpA0ETMlLTLRA5QHwM\/XSswvK9TK9S5tK1Vtqmb5Fz6NlD\/IODc6rF7pUo8o="},"active":{"BOOL":true},"keyByteCount":{"N":"32"},"sk":{"S":"1d7d8961-de9b-4437-80ef-342b8295769a-1"},"accountactive":{"BOOL":true},"created":{"S":"1633092001409.530273"},"algorithm":{"S":"SHA512-ChaChaPoly1305-SHA256"},"modified":{"S":"1633092001409.530273"},"pk":{"S":"PASSWORD|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"},"salt":{"S":"RDhCRTc2RkQtQTQ2My00OTQ3LThBQUUtRTk5ODU4NTE1MzlB"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"username":{"S":"myusername"},"pk":{"S":"USER|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"},"created":{"S":"1633092001413.192139"},"_sk":{"S":"myusername"},"modified":{"S":"1633092001413.192139"},"sk":{"S":"a4d25b32-95b7-456f-8f1d-77a7891c3b76-1"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"created":{"S":"1633092001413.270996"},"pk":{"S":"75830cde-cdd9-4e8d-b4dd-f7030ebef479-1|1|USER|a4d25b32-95b7-456f-8f1d-77a7891c3b76-1|EMAIL"},"sk":{"S":"26b4fb37-36f1-44de-a707-d9e42b31f18e-1"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"address":{"S":"my@email.com"},"_sk":{"S":"my@email.com"},"sk":{"S":"26b4fb37-36f1-44de-a707-d9e42b31f18e-1"},"verified":{"BOOL":false},"created":{"S":"1633092001409.441895"},"modified":{"S":"1633092001409.441895"},"pk":{"S":"EMAIL|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"Item":{"sk":{"S":"1d7d8961-de9b-4437-80ef-342b8295769a-1"},"created":{"S":"1633092001413.308105"},"pk":{"S":"75830cde-cdd9-4e8d-b4dd-f7030ebef479-1|1|USER|a4d25b32-95b7-456f-8f1d-77a7891c3b76-1|PASSWORD"}},"ConditionExpression":"attribute_not_exists(pk)","TableName":"data-global"}},{"Put":{"TableName":"data-global","Item":{"pk":{"S":"_UNIQUE|USER|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"},"sk":{"S":"myusername"},"created":{"S":"1633092001414.284912"}},"ReturnValuesOnConditionCheckFailure":"ALL_OLD","ConditionExpression":"attribute_not_exists(pk)"}},{"Put":{"TableName":"data-global","Item":{"sk":{"S":"my@email.com"},"created":{"S":"1633092001414.472168"},"pk":{"S":"_UNIQUE|EMAIL|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"}},"ReturnValuesOnConditionCheckFailure":"ALL_OLD","ConditionExpression":"attribute_not_exists(pk)"}}],"ClientRequestToken":"E67C8360-DD30-41C0-8336-62C56ACD6593","ReturnConsumedCapacity":"INDEXES"} Response: Status : 400 Headers: [ date : Fri, 01 Oct 2021 12:40:01 GMT x-amzn-requestid : UHNMLGIOQ5IIMGRB1I368K6GG3VV4KQNSO5AEMVJF66Q9ASUAAJG server : Server x-amz-crc32 : 1950075891 content-length : 833 content-type : application/x-amz-json-1.0 connection : keep-alive ] Body: {"__type":"com.amazonaws.dynamodb.v20120810#TransactionCanceledException","CancellationReasons":[{"Code":"None"},{"Code":"None"},{"Code":"None"},{"Code":"None"},{"Code":"None"},{"Code":"None"},{"Code":"None"},{"Code":"ConditionalCheckFailed","Item":{"created":{"S":"1633033335112.123047"},"sk":{"S":"myusername"},"pk":{"S":"_UNIQUE|USER|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"}},"Message":"The conditional request failed"},{"Code":"ConditionalCheckFailed","Item":{"created":{"S":"1633033335111.815918"},"sk":{"S":"my@email.com"},"pk":{"S":"_UNIQUE|EMAIL|1|75830cde-cdd9-4e8d-b4dd-f7030ebef479-1"}},"Message":"The conditional request failed"}],"Message":"Transaction cancelled, please refer cancellation reasons for specific reasons [None, None, None, None, None, None, None, ConditionalCheckFailed, ConditionalCheckFailed]"}

adam-fowler commented 2 years ago

Ok that error should have been picked up by Soto. That'll be a bug. I'll have a look into it.

khinkson commented 2 years ago

Thank you.

adam-fowler commented 2 years ago

@khinkson v5.7.1 of SotoCore has a fix for this

khinkson commented 2 years ago

@adam-fowler Thank you very much. Your work is amazing as usual.