Handle and return errors in lambda hapening on the init phase

0

Hi team,

I used this answer to exit my lambda when an error occur in Redis connect (Thank you @Indranil Banerjee AWS for your answer)

https://repost.aws/questions/QURgqZS0xaQcmfI0PkfT-4QA/handle-errors-in-lambda-hapening-on-the-init-phase

as my code ins in node js/javaScript not python I used process.exit() instead of sys.exit()

but my lambda still fails with a generated error and I can't return back my error response :

return {
    statusCode:"500",
    headers: {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    },
    body: JSON.stringify(MyresponseErrorObject),
  };

Enter image description here

if I do my return

return {
    statusCode:"500",
    headers: {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    },
    body: JSON.stringify(MyresponseErrorObject),
  };

after process.exit() it will be unreachable code.

is process.exit() the right way to exit lambda function in nodeJs (like sys.exit() in python) ?

my lambda still fails as shown in the screenshot above(this is a simulated error I made a mistake on the Redis URL so I can simulate the error and handle it properly outside the event handler code)

appreciate your help on this.

Thank you!

1 Answer
0

Can you please take a look at "Designating a function handler as an ES module" section in https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html

Also take a look at this blog https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

profile pictureAWS
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions