Amazon Web Services - AWS Lambda - 在 Java 8 中等效的回调( "some error type" )



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

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

使用回调参数
Node.js运行时v4.3支持可选的回调参数。你可以使用它显式地将信息返回给调用者。这个一般语法为:

callback(Error error, Object result);

其中:

  • error–是一个可选参数,可用于提供结果Lambda函数执行失败的。当Lambda函数>成功时,可以将null作为第一个参数传递。

  • result–是一个可选参数,可用于提供成功执行函数的结果。提供的结果必须是兼容JSON.stringify。如果提供了错误,则此参数为已忽略。

注意

使用回调参数是可选的。如果您不使用可选的回调参数,其行为与调用callback(),不带任何参数。您可以在中指定回调您的代码将信息返回给调用者。

如果你的代码中不使用回调,AWS Lambda会调用它隐式返回,返回值为null。

当回调被调用(显式或隐式)时,AWS Lambda继续Lambda函数调用,直到Node.js事件循环是空的。

以下是示例回调:

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

AWS Lambda将错误参数的任何非零值视为已处理异常。

只抛出一个异常,不在任何地方捕获它。任何未捕获的异常都会导致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声明,它允许将未处理的异常抛出到方法之外。

最新更新