gpt4 book ai didi

java - AWS Lambda - 回调 ("some error type") 相当于 Java 8

转载 作者:搜寻专家 更新时间:2023-11-01 02:04:04 24 4
gpt4 key购买 nike

如何让 lambda 函数在 Java 8 中报告失败?

我发现这在 Node.js 中是可能的。
http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Using the Callback Parameter
The Node.js runtime v4.3 supports the optional callback parameter. You can use it to explicitly return information back to the caller. The general syntax is:

callback(Error error, Object result);

Where:

  • error – is an optional parameter that you can use to provide results of the failed Lambda function execution. When a Lambda function >succeeds, you can pass null as the first parameter.

  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.

Note

Using the callback parameter is optional. If you don't use the optional callback parameter, the behavior is same as if you called the callback() without any parameters. You can specify the callback in your code to return information to the caller.

If you don't use callback in your code, AWS Lambda will call it implicitly and the return value is null.

When the callback is called (explicitly or implicitly), AWS Lambda continues the Lambda function invocation until the Node.js event loop is empty.

The following are example callbacks:

callback(); // Indicates success but no information returned to the caller. callback(null); // Indicates success but no information returned to the caller. callback(null, "success"); // Indicates success with information returned to the caller. callback(error);
// Indicates error with error information returned to the caller.

AWS Lambda treats any non-null value for the error parameter as a handled exception.

最佳答案

只是抛出一个异常,不要在任何地方捕获它。任何未捕获的异常都会导致 Lambda 失败。您可以查看有关如何使用 Java 在 AWS Lambda 中报告故障的更多信息:https://docs.aws.amazon.com/lambda/latest/dg/java-exceptions.html

public TestResponse handleRequest(TestRequest request, Context context) throws RuntimeException {
throw new RuntimeException("Error");
}

注意 throws 声明,它允许抛出一个未处理的异常到方法之外。

关于java - AWS Lambda - 回调 ("some error type") 相当于 Java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com