0
0
DynamoDBquery~10 mins

Transaction error handling in DynamoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction in DynamoDB.

DynamoDB
const params = { TransactItems: [[1]] };
Drag options to blanks, or click blank then click option'
A{ Get: { TableName: 'Users', Key: { UserId: '123' } } }
B{ Put: { TableName: 'Users', Item: { UserId: '123' } } }
C{ Delete: { TableName: 'Users', Key: { UserId: '123' } } }
D{ Update: { TableName: 'Users', Key: { UserId: '123' }, UpdateExpression: 'SET Age = :age' } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Get operation inside TransactItems which is not supported.
Forgetting to wrap the operation inside an object with the operation name as key.
2fill in blank
medium

Complete the code to catch a transaction cancellation error.

DynamoDB
try {
  await dynamodb.transactWrite(params).promise();
} catch (error) {
  if (error.code === [1]) {
    console.log('Transaction cancelled');
  }
}
Drag options to blanks, or click blank then click option'
A'InternalServerError'
B'ConditionalCheckFailedException'
C'ProvisionedThroughputExceededException'
D'TransactionCanceledException'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConditionalCheckFailedException which is for condition failures, not transaction cancellation.
Checking for wrong error codes that do not relate to transactions.
3fill in blank
hard

Fix the error in the transaction retry logic by completing the blank.

DynamoDB
let retries = 0;
const maxRetries = 3;
while (retries < maxRetries) {
  try {
    await dynamodb.transactWrite(params).promise();
    break;
  } catch (error) {
    if (error.code === 'TransactionCanceledException' && retries < [1]) {
      retries++;
    } else {
      throw error;
    }
  }
}
Drag options to blanks, or click blank then click option'
AmaxRetries
B5
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a hardcoded number that does not match maxRetries.
Using zero which prevents any retries.
4fill in blank
hard

Fill both blanks to check if the transaction error is retryable and log the error message.

DynamoDB
if (error.code === [1]) {
  console.log('Retryable error:', error.[2]);
}
Drag options to blanks, or click blank then click option'
A'TransactionCanceledException'
Bmessage
Ccode
Dstack
Attempts:
3 left
💡 Hint
Common Mistakes
Logging the error code instead of the message.
Checking for wrong error codes.
5fill in blank
hard

Fill all three blanks to build a transaction with condition check, put, and handle cancellation error.

DynamoDB
const params = {
  TransactItems: [
    { ConditionCheck: { TableName: 'Orders', Key: { OrderId: '001' }, ConditionExpression: [1] } },
    { Put: { TableName: 'Orders', Item: { OrderId: '002', Status: 'Pending' } } }
  ]
};

try {
  await dynamodb.transactWrite(params).promise();
} catch (error) {
  if (error.code === [2]) {
    console.log('Transaction failed:', error.[3]);
  } else {
    throw error;
  }
}
Drag options to blanks, or click blank then click option'
A'attribute_not_exists(OrderId)'
B'TransactionCanceledException'
Cmessage
D'attribute_exists(OrderId)'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'attribute_exists' which would fail if the item does not exist.
Logging the error code instead of the message.