首页 文章

API Gateway Lambda无法处理异常

提问于
浏览
0

我有基于Python的Lambda,它与API网关集成,并尝试处理下面的lambda异常 .

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

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

enter image description here

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

enter image description here

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

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

1 回答

  • 0

    我自己找到了解决方案 .

    这是我的Lambda代码:

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

    API网关将收到类似的错误

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

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

    我改变了下面的lambda错误正则表达式后,一切正常

    .*'errorType': 'Exception'.*
    

相关问题