Complete the code to start a transaction in DynamoDB.
const params = { TransactItems: [[1]] };The TransactItems array requires transaction operations like Put. Here, we start with a Put operation.
Complete the code to catch a transaction cancellation error.
try { await dynamodb.transactWrite(params).promise(); } catch (error) { if (error.code === [1]) { console.log('Transaction cancelled'); } }
The error code TransactionCanceledException indicates a transaction was cancelled in DynamoDB.
Fix the error in the transaction retry logic by completing the blank.
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; } } }
The retry condition should compare retries with maxRetries to limit retry attempts properly.
Fill both blanks to check if the transaction error is retryable and log the error message.
if (error.code === [1]) { console.log('Retryable error:', error.[2]); }
Check for TransactionCanceledException to identify retryable errors and log the message property for details.
Fill all three blanks to build a transaction with condition check, put, and handle cancellation error.
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;
}
}The condition expression ensures the order does not exist before putting a new one. The error code TransactionCanceledException is caught and the error message is logged.