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 回答
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
专家
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则