我有一个带有 LAMBDA_PROXY 集成请求类型的 API 网关。来电后 context.succeed
在 Lambda 中,响应 header 被发送回代码 302
正如预期的那样(如下所示)。但是,我想处理500
和 404
错误,到目前为止我唯一确定的是,当我收到 502 Bad Gateway
时,我错误地返回了错误。 .我的 context.fail
出了什么问题?
这是我的 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
}
});
}
});
};
最佳答案
如果您在 Lambda 函数(或 context.fail)中抛出异常,API Gateway 会读取它,就像您的后端出现问题并返回 502。如果这是您期望的运行时异常并希望返回 500/404,将 context.succeed 方法与您想要的状态代码和消息一起使用:
if (parsed.error) {
let error = {
statusCode: 404,
headers: { "Content-Type": "text/plain" } // not sure here
body: new Error(parsed.error)
}
return context.succeed(error);
关于amazon-web-services - AWS API 网关错误响应生成 502 "Bad Gateway",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42847945/