有没有什么办法可以在不引发异常的情况下通知Java的AWS Lambda中的错误? [英] Is there any way to signal an error in AWS Lambda for Java without throwing an exception?

查看:82
本文介绍了有没有什么办法可以在不引发异常的情况下通知Java的AWS Lambda中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的Lambda抛出Exception和消息404,则API网关中的响应为

If my Lambda throws an Exception with the message 404 then the response as seen in API Gateway is

{
  "errorMessage":"404",
  "errorType":"java.lang.Exception",
  "stackTrace":[..."]
}

我可以在errorMessage上进行匹配以影响HTTP结果.

and I can match on the errorMessage to affect the HTTP result.

但是,如果我有效地返回了相同的结果,那就是

However if I return effectively the same result, viz:

{
  "errorMessage":"404",
  "errorType":"Error"
}

API网关似乎无法识别出错误,并且始终返回200.

API Gateway doesn't seem to recognise that there is an error and always returns a 200.

我的功能强大的代码是否有任何方法可以在不引发异常的情况下发出错误信号?

Is there any way for my nice functional code to signal an error without throwing an exception?

推荐答案

为了评估响应模式,Lambda函数必须退出并带有错误-无法在成功的Lambda响应中简单地返回"errorMessage"字段来伪造"错误响应.

The Lambda function must exit with an error in order for the response pattern to be evaluated – it is not possible to "fake" an error response by simply returning an "errorMessage" field in a successful Lambda response.

https ://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/

这篇关于有没有什么办法可以在不引发异常的情况下通知Java的AWS Lambda中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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