首页 文章

Amazon API Gateway和AWS Lambda中的错误处理模式

提问于
浏览
0

我创建了一个简单的lambda函数,其代码如下 .

exports.handler = (event, context, callback) => {
    const operation = event.body.operation;
    console.log("operation = ", operation)
    switch (operation) {
        case 'add': callback(null, 'post method');
            break;
        case 'add1': callback(null, {
            status: 0,
            errorType: "InternalServerError",
            errorCode: "001",
            errorMessage: "post method error."
        }
        );
        default: callback(null, 'Hello from Lambda');
            break;
    }
};

它将与Amazon API Gateway连接 . 使用能够获得成功和错误响应的REST客户端 . 但是HTTP状态代码仍然是200.然后我以两种方式修改了API网关集成响应 .

1. Selection pattern : “InternalServerError”
 2. Selection pattern : “.*InternalServerError”
    Method response : 500

但我仍然有200个HTTP状态代码 . 与此选择模式相关的实际问题是什么?

2 回答

  • 0

    当使用context.fail()从Lambda函数抛出错误时,API Gateway会检查错误模式 . 有关在API GW中处理Lambda错误的更多详细信息,请参阅此article .

  • 1

    在您的情况下,您需要从我的回答here返回正确的HTTP响应:

相关问题