How to handle Error on AWS node

how do I return error on aws lambda using node
I tried the code from document but its returning statuscode 200 and I dont see error message anywhere in the response

module.exports.hello = (event, context, callback) => {
  callback(new Error('[404] Not found'));

any idea what am I missing here ?

I assume you’re talking about a Lambda backing the API Gateway using Lambda proxy integration. Something like this will do it:

callback(null, { statusCode: 404, body: JSON.stringify({ message: "Not found" }) });

You should get a 404 error response with the body { message: "Not found" }.

1 Like

thanx that worked, i thought we have to callback(new Error(’[404] Not found’));

There are two ways to integrate the API Gateway and Lambda. The default used by Serverless is Lambda Proxy Integration. For that you need to return an object telling the API Gateway how to respond. It’s the easiest to use so if you’re new I’d recommend sticking to it.

The more complicated Lambda Integration uses the callback error parameter to trigger different responses.

1 Like