Reputation: 1951
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
}
});
}
});
};
Upvotes: 13
Views: 62339
Reputation: 432
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/
Upvotes: 2
Reputation: 1183
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.
Upvotes: 2
Reputation: 1915
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, {});
Upvotes: 2
Reputation: 1024
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);
Upvotes: 20