2

Every time I have a syntax error or I just want to send a custom error in my AWS Lambda function, I get the same 502 Bad Gateway response (Internal server error).

I tried that simple code:

module.exports.saveImage = (event, context, callback) => {
    callback("the sky is falling!"); // also tried sending new Error("the sky is falling!")
}

And still getting the same "Internal server error" response instead of the defined one.

This is my function in the serverless.yml file:

saveImage:
  handler: handler.saveImage
  environment:
    BUCKET: ${self:custom.bucket}
  events:
  - http:
      path: saveImage
      method: post
      cors: true,
      integration: lambda-proxy

May I have misunderstood something from this article? It seems to recieve the "errorMessage": "the sky is falling!" in the API Gateway response (and that's what I would expect).

https://aws.amazon.com/es/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/

2
  • 1
    Did you configure the API Gateway's Integration Response section to handle the Lambda's response? Commented Aug 8, 2017 at 0:43
  • I've updated my question with the serverless.yml config. By the way, I can receive the response if I put the error in the second argument, like this: callback(null, { body: JSON.stringify( { errorMessage: "my error" }) }); Is that how we should handle errors? I thought we can use the first argument to send errors. Commented Aug 8, 2017 at 11:20

1 Answer 1

4

If you use integration: lambda-proxy, you need to return a proper error response from your Lambda, not from API Gateway.

In this case, you can use what already tried:

callback(null, { body: JSON.stringify( { errorMessage: "my error" })

I thought we can use the first argument to send errors

You can, if you use integration: lambda in your serverless.yml but in your case, you're not.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'll have a look at the differences within them and if it is worth the change.
It's a matter of preference and each has its own pros and cons. See stackoverflow.com/q/42474264/1252647.

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.