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
  }
}