Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to find all the exception classes in AWS-SDK for dynamodb in NodeJS typescript?

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')
  }
}
like image 881
sravan ganji Avatar asked Apr 05 '19 14:04

sravan ganji


People also ask

What is DynamoDBClient?

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.

Which command may be used to import the AWS SDK for JavaScript into a JavaScript module?

The preferred way to install the AWS SDK for JavaScript for Node. js is to use npm, the Node. js package manager .

How do I install AWS SDK client DynamoDB?

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.

What causes DynamoDB system errors?

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.


2 Answers

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;
    }
}
like image 167
HaaLeo Avatar answered Sep 17 '22 13:09

HaaLeo


Udpate: aws-sdk v3

Since version v3 the property name has changed to name:

if (e.name === 'ConditionalCheckFailedException') {

Old post: aws-sdk v2

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.

like image 22
Kim Kern Avatar answered Sep 21 '22 13:09

Kim Kern