简体   繁体   English

有没有办法在AWS Lambda for Java中发出错误信号而不抛出异常?

[英]Is there any way to signal an error in AWS Lambda for Java without throwing an exception?

If my Lambda throws an Exception with the message 404 then the response as seen in API Gateway is 如果我的Lambda使用消息404抛出Exception ,那么API网关中的响应就是

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

and I can match on the errorMessage to affect the HTTP result. 我可以匹配errorMessage以影响HTTP结果。

However if I return effectively the same result, viz: 但是,如果我有效地返回相同的结果,即:

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

API Gateway doesn't seem to recognise that there is an error and always returns a 200. API网关似乎没有意识到存在错误并始终返回200。

Is there any way for my nice functional code to signal an error without throwing an exception? 有没有办法让我的好功能代码在不抛出异常的情况下发出错误信号?

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. Lambda函数必须以错误退出,以便评估响应模式 - 只需在成功的Lambda响应中返回“errorMessage”字段,就不可能“伪造”错误响应。

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

It is more of a generic question rather than Java-related as AWS API Gateway integration has nothing much to do with the language in which AWS Lambda function is written. 它更像是一个通用问题而不是Java相关问题,因为AWS API Gateway集成与编写AWS Lambda函数的语言没有多大关系。

You can achieve returning custom error message by following the guidelines from the AWS documentation here: Handle Custom Lambda Errors in API Gateway 您可以按照以下AWS文档中的准则来实现返回自定义错误消息: 处理API网关中的自定义Lambda错误

For Lambda custom integrations, you must map errors returned by Lambda in the integration response to standard HTTP error responses for your clients. 对于Lambda自定义集成,您必须将集成响应中Lambda返回的错误映射到客户端的标准HTTP错误响应。 Otherwise, Lambda errors are returned as 200 OK responses by default and the result is not intuitive for your API users. 否则,默认情况下,Lambda错误将返回200 OK响应,结果对于API用户而言并不直观。

There are two types of errors that Lambda can return: Lambda可以返回两种类型的错误:

  1. Standard Errors 标准错误
  2. Custom Errors 自定义错误

In your API, you must handle these differently. 在API中,您必须以不同方式处理这些问题。

With the Lambda proxy integration, Lambda is required to return an output of the following format: 使用Lambda代理集成时,Lambda需要返回以下格式的输出:

{
  "isBase64Encoded" : "boolean",
  "statusCode": "number",
  "headers": { ... },
  "body": "JSON string"
}

In this output, statusCode is typically 4XX for a client error and 5XX for a server error. 在此输出中,对于客户端错误,statusCode通常为4XX,对于服务器错误,statusCode通常为5XX。 API Gateway handles these errors by mapping the Lambda error to an HTTP error response, according to the specified statusCode. API Gateway根据指定的statusCode将Lambda错误映射到HTTP错误响应,从而处理这些错误。 For API Gateway to pass the error type (for example, InvalidParameterException), as part of the response to the client, the Lambda function must include a header (for example, "X-Amzn-ErrorType":"InvalidParameterException") in the headers property. 对于API网关传递错误类型(例如,InvalidParameterException),作为对客户端的响应的一部分,Lambda函数必须在标头中包含标头(例如,“X-Amzn-ErrorType”:“InvalidParameterException”)属性。

For more details, check out this official document from AWS here: Handle Lambda Errors in API Gateway 有关更多详细信息,请在此处查看AWS的此官方文档: 处理API网关中的Lambda错误

I think this is more a design question rather than a Java/AWS question. 我认为这更像是一个设计问题而不是Java / AWS问题。

The intended design of AWS lambda is ... when your code has internal problems, it will return 500 (the ugly ones) while if the client is trying to reach something that's not there (400 or 300). AWS lambda的预期设计是......当您的代码存在内部问题时,如果客户端尝试到达不存在的东西(400或300),它将返回500(丑陋的)。 This is true regardless of framework (same for Java Spring and Python Django etc.) 无论框架如何都是如此(对于Java Spring和Python Django等也是如此)

If your function is nicely detecting an error and returning a perfect object (json), then it is (by definition) not an internal problems of your code. 如果你的函数很好地检测到错误并返回一个完美的对象(json),那么(根据定义)它不是代码的内部问题。 In other words, your code is functioning as intended. 换句话说,您的代码按预期运行。 As far as lambda is concerned, it is 200 ok. 就lambda而言,它是200 ok。

If you want ugly response (5xx), you need to throw an error/exception. 如果你想要丑陋的响应(5xx),你需要抛出一个错误/异常。 Else, lambda will think your code is fine and whatever error you are throwing inside your json is normal payload. 否则,lambda会认为你的代码很好,而你在json中抛出的任何错误都是正常的有效负载。 It is not to say you should not encode your error in json response. 这并不是说你不应该在json响应中编码你的错误。 It is perfectly normal to include application-specific/business-logic driven errors to pass in payload like this. 包含特定于应用程序/业务逻辑驱动的错误以传递这样的有效负载是完全正常的。

I hope this answer your question. 我希望这能回答你的问题。 In short, 简而言之,

there is no other way to have lambda return 5xx than throwing an error and it is intended behavior. 没有其他方法让lambda返回5xx而不是抛出错误,这是预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM