Complete the code to catch a DynamoDB exception.
try { // some DynamoDB operation } catch ([1] e) { console.log('Error:', e.message); }
The standard error type to catch exceptions in JavaScript is Error. DynamoDB client errors are instances of Error.
Complete the code to retry a DynamoDB operation after catching a throttling error.
if (error.code === [1]) { // retry logic here }
The error code ThrottlingException indicates the request was throttled and can be retried.
Fix the error in the retry logic to wait before retrying.
async function retryOperation() {
try {
await dynamoDbClient.send(command);
} catch (error) {
if (error.code === 'ThrottlingException') {
await new Promise(resolve => setTimeout([1]));
return retryOperation();
}
throw error;
}
}The setTimeout function expects a callback and a delay in milliseconds. The correct syntax is setTimeout(() => resolve(), 1000).
Fill both blanks to implement exponential backoff in retry delay.
async function retryWithBackoff(attempt) {
try {
await dynamoDbClient.send(command);
} catch (error) {
if (error.code === 'ThrottlingException' && attempt < 5) {
const delay = [1] * Math.pow(2, [2]);
await new Promise(resolve => setTimeout(() => resolve(), delay));
return retryWithBackoff(attempt + 1);
}
throw error;
}
}Exponential backoff uses a base delay (e.g., 100 ms) multiplied by 2 to the power of the attempt number.
Fill all three blanks to log error details and retry with a max attempt limit.
async function safeRetry(attempt) {
try {
await dynamoDbClient.send(command);
} catch (error) {
console.error('Error code:', [1]);
console.error('Error message:', [2]);
if (error.code === 'ThrottlingException' && [3] < 3) {
await new Promise(resolve => setTimeout(() => resolve(), 200));
return safeRetry(attempt + 1);
}
throw error;
}
}Use error.code and error.message to log error details. The retry limit is controlled by the attempt variable.