[英]AWS Lambda - callback(“some error type”) equivalent in Java 8
如何在Java 8中使lambda函数报告失败?
我在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 to
callback(); // Indicates success but no information returned to
the caller. callback(null); // Indicates success but no information
the caller. callback(null); // Indicates success but no information
the caller. callback(null); // Indicates success but no information
returned to the caller. callback(null, "success"); // Indicates
returned to the caller. callback(null, "success"); // Indicates
returned to the caller. callback(null, "success"); // Indicates
success with information returned to the caller. callback(error);
success with information returned to the caller. callback(error);
// Indicates error with error information returned to the caller.
AWS Lambda将error参数的任何非null值视为已处理的异常。
只是抛出异常而不是在任何地方捕获它。 任何未捕获的异常都会导致Lambda失败。 您可以在以下网址中查看有关如何使用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
声明,它允许从方法中抛出未处理的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.