【问题标题】:Error handling patterns in Amazon API Gateway and AWS LambdaAmazon API Gateway 和 AWS Lambda 中的错误处理模式
【发布时间】:2017-06-23 08:16:37
【问题描述】:

我创建了一个具有以下代码的简单 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 Gateway 集成响应。

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

但我仍然得到 200 HTTP 状态码。与此选择模式相关的实际问题是什么?

【问题讨论】:

  • 您是否需要使用context.fail()? (向下滚动过去接受的答案并阅读其他答案。)

标签: node.js rest amazon-web-services aws-lambda aws-api-gateway


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 2018-01-15
      • 2021-01-08
      • 2016-06-02
      • 2017-02-23
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多