8
votes

I have an API Gateway with a LAMBDA_PROXY Integration Request Type. Upon calling context.succeed in the Lambda, the response header is sent back with code 302 as expected (shown below). However, I want to handle 500 and 404 errors, and the only thing I am sure about so far, is that I am returning the error incorrectly as I am getting 502 Bad Gateway. What is wrong with my context.fail?

Here is my handler.js

const handler = (event, context) => { 
    //event consists of hard coded values right now
    getUrl(event.queryStringParameters)
    .then((result) => {
        const parsed = JSON.parse(result);
        let url;
        //handle error message returned in response
        if (parsed.error) {
            let error = {
                statusCode: 404,
                body: new Error(parsed.error)
            }
            return context.fail(error);
        } else {
            url = parsed.source || parsed.picture;
            return context.succeed({
                statusCode: 302,
                headers: {
                  Location : url
                }
              });
        }
    });
};
4

4 Answers

14
votes

If you throw an exception within the Lambda function (or context.fail), API Gateway reads it as if something had gone wrong with your backend and returns 502. If this is a runtime exception you expect and want to return a 500/404, use the context.succeed method with the status code you want and message:

if (parsed.error) {
  let error = {
    statusCode: 404,
    headers: { "Content-Type": "text/plain" } // not sure here
    body: new Error(parsed.error)
}
return context.succeed(error);
1
votes

I had the same problem, in my case the issue was that my function was not returning anything in context.done(). So instead of context.done(null), I did context.done(null, {});

1
votes

I've gotten 502's from multiple things. Here are the ones I have figured out so far.

Answer 1:

claudia generate-serverless-express-proxy --express-module {src/server?} If you are not using claudia and express, this answer won't help you.

Answer 2:

Lambda function->Basic Settings->Timeout. Increase it to something reasonable. It defaults to 3 seconds. But the first time building it typically takes longer.

0
votes

I had a problem like this, I was returning JSON as a JavaScript Object in the body, but you are supposed to return it as a string. All I had to do was do a JSON.stringify(dataobject) to convert the JSON into a string before returning it.

https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/