AWS NodeJS SDK Exception Type Checking By Example

We often need check type of Exception thrown by the SDK.

For example, check whether your putItem() has thrown an ConditionalCheckFailedException, so you know that your attribute_not_exists check failed and a duplicate exists in the DynamoDB table.

For Java it is easy to check the type of an Exception. However, in JavaScript, exceptions are not differentiated by different classes, but instead with a code property.

try {
  // putItem() with ConditionExpression
} catch (e) {
  if (e.code === 'ConditionalCheckFailedException') {
    // do fancy stuff
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s