0

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 Answer 1

1

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;
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

in all honesty...i have no clue what you are trying to do :)
just trying to get the error in the logs, very basic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.