首页 文章

即使Lambda返回错误,AWS API Gateway也会返回200

提问于
浏览
2

我正在构建一个AWS API邮件服务,它基本上检查请求的主体,然后发送包含该主体数据的邮件 . 它检查是否存在所有必需参数,并在数据符合我们要求的情况下进行一些基本检查 .

问题是,即使Lambda函数抛出错误(我使用Lambda测试接口验证),API网关也会返回 200 响应代码 with the error object as body

这意味着我得到这样的日志:

Tue Apr 11 14:23:43 UTC 2017 : Method response body after transformations: 
{"errorMessage":"\"[BadRequest] Missing email\""}
Tue Apr 11 14:23:43 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=Root=************, Content-Type=application/json}
Tue Apr 11 14:23:43 UTC 2017 : Successfully completed execution
Tue Apr 11 14:23:43 UTC 2017 : Method completed with status: 200

由于最后一部分,我相信API Gateway正在返回 200 .

我做了几件事来设置错误处理:

  • 我为错误代码添加了第二个Method响应:
    enter image description here

  • 我为错误代码添加了第二个Integration响应:
    enter image description here

此时我不确定为什么它仍然无法返回正确的响应 . 我检查了关于此的各种帖子(包括:Is there a way to change the http status codes returned by Amazon API Gateway?How to return error collection/object from AWS Lambda function and map to AWS API Gateway response code)并阅读文档 .

我也尝试了'Lambda代理方式',但这并没有产生任何结果(lambda在这方面没有正确执行' .

有谁看到我在这里失踪了?

1 回答

  • 1

    我看到一些可能导致问题的事情 .

    您的错误消息引用: "\"[BadRequest] Missing email\"" ,因此 ^[BadRequest] 正则表达式与错误字符串不匹配 . 在我运行的简单测试中,我不得不逃避 [] (即 \[\] ),因为方括号是为字符类保留的 .

    在不更改errorMessage格式的情况下,这样的模式应该可以正常工作: ^"\[BadRequest\].*

相关问题