【问题标题】:API Gateway + Lambda failed to handle exceptionAPI Gateway + Lambda 处理异常失败
【发布时间】:2018-12-06 02:32:54
【问题描述】:

我有基于 Python 的 Lambda,它与 API Gateway 集成,并尝试在下面处理 lambda 异常。

{
  "stackTrace": [
    [
      "/var/task/index.py",
      14,
      "handler",
      "raise Exception(err)"
    ]
  ],
  "errorType": "Exception",
  "errorMessage": "device name existed"
}

我已经为异常定义了方法响应的 HTTP 状态 400。

我还定义了“Lambda Error Regex”和“Body Mapping Templates”。

但是当我的 lambda 引发异常时,API 网关仍然响应 HTTP 状态 200 而不是 400。

任何人都知道出了什么问题。我错过了什么?

【问题讨论】:

    标签: python exception-handling aws-lambda aws-api-gateway


    【解决方案1】:

    我自己找到了解决办法。

    这是我的 Lambda 代码:

        raise Exception({
            "errorType" : "Exception",
            "httpStatus": 400,
            "message": err
        })
    

    API Gateway 会收到类似的错误

    {
       "stackTrace": [["/var/task/index.py", 17, "handler", "\"message\": err"]], 
       "errorType": "Exception", 
       "errorMessage": "{'message': 'device not found', 'errorType': 'Exception', 'httpStatus': 400}"
    }
    

    我的 lambda 错误正则表达式不起作用的原因是错误消息在单引号内而不是双引号内。

    如下所示更改 lambda 错误正则表达式后,一切正常

    .*'errorType': 'Exception'.*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2021-06-16
      • 2020-03-18
      • 2017-07-28
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多