I am trying to insert some data into dynamodb, and as expected I am getting a ConditionalCheckFailedException
. So I am trying to catch that exception for only that scenario, apart from that I want to throw server error for all other errors.
But to add the type, I am not able to find the ConditionalCheckFailedException
in aws-sdk.
This is what I was trying to do.
// where to import this from
try {
await AWS.putItem(params).promise()
} catch (e) {
if (e instanceof ConditionalCheckFailedException) { // unable to find this exception type in AWS SDK
throw new Error('create error')
} else {
throw new Error('server error')
}
}
Since version v3 the property name has changed to name
:
if (e.name === 'ConditionalCheckFailedException') {
You can check the error by using the following guard instead:
if (e.code === 'ConditionalCheckFailedException') {
Note that instanceof
only works on classes, not on interfaces. So even if you had the type, if it was an interface you couldn't use it because it relies on certain prototype checks. Using the err.code
property is safer.