2016-08-02 4 views
3

ラムダ関数がJava 8の失敗を報告するにはどうすればよいですか?AWS 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.

+0

関連https://stackoverflow.com/questions/45108530/is-there-any-way-to-signal-an-error-in-aws-lambda-for-java-without-throwing-an-e –

答えて

4

ただ、例外をスローし、どこでもそれをキャッチしないでください。キャッチされていない例外があれば、ラムダは失敗します。あなたは、JavaとAWSラムダの失敗を報告する方法についての詳細を確認することができます。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宣言に注意してください。

+0

それが私の問題です。 handleRequestメソッドで例外をスローしようとした場合。私はコンパイルエラーが発生しています: "未処理例外タイプ例外"。私は例外を処理したくない。 Eclipseは、試して/キャッチでそれをラップするように強制しています – user1932634

+0

Javaの基本:) throws文でハンドラメソッドをマークし、コンパイラが鳴りません "TestResponse handleRequest(TestRequestリクエスト、コンテキストコンテキスト)throws RuntimeException" –

+0

美しい!どうもありがとうございます!! – user1932634

関連する問題