API Gateway 500对未处理异常的响应 [英] API Gateway 500 Response on Unhandled Exception

查看:865
本文介绍了API Gateway 500对未处理异常的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在API网关上有一个与Python Lambda函数集成的方法.我希望我的API网关返回500响应,如果我的Python Lambda函数中引发了异常,其中errorMessage字段与该方法的集成响应"中的正则表达式之一不匹配.

I have a method on an API gateway which integrates with a Python Lambda function. I would like my API Gateway to return a 500 response if an exception is raised in my Python Lambda function where the errorMessage field doesn't match one of the regex's in the Integration Response's for the method.

我只想在未引发异常并且lambda返回没有失败的情况下返回200.

I would only like to return a 200 if an exception is not raised and the lambda returns without failure.

使用上图中的设置-引发的任何与400响应都不匹配(.|\n)*'type':\s*'.*Error'(.|\n)*的异常将给出200响应.

With the setup in the picture above - any exception raised which does not match (.|\n)*'type':\s*'.*Error'(.|\n)* for the 400 response will give a 200 response.

如果引发了任何与已配置的响应都不匹配的异常,如何返回500-当Python函数成功返回时仍返回200?我是否只需要将所有内容包装在我的代码中(使用try除外),这可能会引发异常并生成可预测的errorMessage字符串?

How can I return 500 if there is any exception raised which does not match an already configured response - while still returning 200 when the Python function returns successfully? Do I just have to wrap everything in my code (with try except) which could possibly raise an exception and generate a predictable errorMessage string?

我目前正在通过具有500的Lambda错误正则表达式为(.|\n)*的集成响应来实现这一目标".将未处理的异常捕获为500个错误,这是一种合理的方法吗?

I am currently 'achieving this' by having an Integration Response for 500 with a Lambda Error Regex of (.|\n)*. Is this a reasonable way to catch unhandled exceptions as 500 errors?

事实证明,当Python函数返回无异常时,此配置给出500个错误.

It turns out this configuration is giving 500 errors when the Python function returns without Exception.

我希望Lambda错误正则表达式仅在Python函数引发实际异常时才尝试匹配(因为它们仅根据文档检查errorMessage字段).使它退回到默认方法201(在更新的方案中).奇怪的是,在使用API​​ GW控制台测试"进行测试时,它返回400,但是在其他任何地方尝试时,则返回500(具有在500 Integration Response中定义的主体映射).

I would expect the Lambda Error Regex's to only attempt to match if an actual exception is raised by the Python function (as they only check the errorMessage field according to the documentation). Causing it to fall back on the default method of 201 (in the updated scenario). Oddly, it returns 400 when testing with the API GW console 'Test', but gives 500 (with the body mapping defined in the 500 Integration Response) when trying from anywhere else.

我想返回所有未处理的异常(因为500)的唯一方法是从我的Python函数返回raise Exception('Success')而不是return-然后让201 Lambda Error Regex匹配成功" ...我真的宁愿不这样做.

The only way I can think to return all unhandled exceptions as 500 is to raise Exception('Success') instead of return from my Python function - then have the 201 Lambda Error Regex match 'Success'... Which I'd really rather not do.

推荐答案

鉴于API网关错误响应处理机制的局限性,您当前的方法是合理的.

Your current approach is reasonable given the limitations of API Gateway's error response handling mechanism.

API网关团队已收到多个客户的反馈,表明我们的错误处理支持存在缺陷.我们正在积极致力于改善这一状况,并希望在将来提供改进的错误处理体验.

The API Gateway team has received feedback from multiple customers indicating the shortcomings of our error handling support. We are actively working on improving this and hope to deliver an improved error handling experience in the future.

这篇关于API Gateway 500对未处理异常的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆