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')
}
}
Class DynamoDBClient DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database, so that you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.
The preferred way to install the AWS SDK for JavaScript for Node. js is to use npm, the Node. js package manager .
Installing. To install the this package, simply type add or install @aws-sdk/client-dynamodb using your favorite package manager: npm install @aws-sdk/client-dynamodb.
The most likely cause is an invalid AWS access key ID or secret key. This error can occur for several reasons, such as a required parameter that is missing, a value that is out of range, or mismatched data types.
When you are using aws-sdk v3 the error does not have a property code
. Instead, you want to check error.name
.
For example:
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
const key = 'myKey.json';
const s3Client = new S3Client({});
const command = new GetObjectCommand({
Bucket: 'bucketName',
Key: key
});
try {
const response = await s3Client.send(command);
} catch (error) {
if (error.name === 'NoSuchKey') {
console.warning(`My key="${key}" was not found.`);
} else {
throw 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With