0
votes

I have the following Lambda which tries to scan a table. On purpose the table does not exists, as I want to catch the error and handle it. But, it seems it is just being ignored.
how can I see the errors (I assume there should be an error)
Lambda:

const AWS = require("aws-sdk");
let response;
const dyDocClient = new AWS.DynamoDB.DocumentClient();

exports.lambdaHandler = async (event, context) => {
    
    const params = {
        TableName: "tttttt"
    };
    console.log('The table name is ',params.TableName);

    try {

        dyDocClient.scan(params, (err, data)=>{
            
            if (err) {
                console.log("Unable to scan the table. Error JSON:", JSON.stringify(err));
            } else {
                console.log("Doing baba dance!");
            }
            return 2;

        });

        response = {
            'statusCode': 200,
            'body': JSON.stringify({
                message: 'received'
            })
        }

    } catch (err) {
        console.log('Error was captured');
        console.error(JSON.stringify(err));
        return err;
    }

    return response
};

What I see in the cloudwatch log:

2021-09-07T12:20:25.972-04:00 START RequestId: d7858877-68d5-4a68-ab0b-0d14efd61712 Version: $LATEST 2021-09-07T12:20:25.975-04:00 2021-09-07T16:20:25.975Z d7858877-68d5-4a68-ab0b-0d14efd61712 INFO The table name is tttttt
2021-09-07T12:20:26.498-04:00 END RequestId:
d7858877-68d5-4a68-ab0b-0d14efd61712 2021-09-07T12:20:26.498-04:00 REPORT RequestId: d7858877-68d5-4a68-ab0b-0d14efd61712 Duration: 524.08 ms Billed
Duration: 525 ms Memory Size: 128 MB Max Memory Used: 90 MB Init Duration: 592.69 ms

Not sure this is relevant, but I invoke the lambda via api gateway

1

1 Answers

1
votes

this is a bit of an async issue; the below should work, but using async/await

const AWS = require("aws-sdk");
const dyDocClient = new AWS.DynamoDB.DocumentClient();

exports.lambdaHandler = async (event, context) => {

  const params = {
    TableName: "tttttt"
  };
  console.log('The table name is ', params.TableName);

  try {

    let data = await dyDocClient.scan(params).promise().catch(err => {
      console.log("Unable to scan the table. Error JSON:", JSON.stringify(err));
      throw err
    })
    console.log("Doing baba dance!");
    return {
      'statusCode': 200,
      'body': JSON.stringify({
        message: 'received'
      })
    }
  } catch (err) {
    console.log('Error was captured');
    console.error(JSON.stringify(err));
    return err;
  }
};