I'm trying to handle an error in a Lambda function written in Go.
The Lambda is triggered by API Gateway.
When I respond with a 200, I get the correct response.
But when I respond with a 500 code, I always receive {"message": "Internal server error"}
Here is part of the code:
func newErrReponse(message string) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
Body: message,
StatusCode: 500,
}, errors.New(message)
}
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return newErrReponse("some error")
}
func main() {
lambda.Start(handleRequest)
}
I was expecting "some error", but I always get the internal server error. I tried JSON in the body but that didn't help. The integration request is of type LAMBDA_PROXY. That was the default.
How can I control the error response?
errors.New(message)seems likely to be the issue -- your function didn't fail, it succeeded. You only want to return an error response for API Gateway to deliver back to the client, not indicate that an exception has occurred. Note the way it's done in Node:callback(null, {"statusCode": 400, "body": "Missing parameters of ..."}), wherenullis the (non-)"error." docs.aws.amazon.com/apigateway/latest/developerguide/…