Java function error handling
If a handler reports a function runtime or loading error, the runtime environment automatically captures the error and returns a JSON document with the error type in the response. For more information about the JSON document format, see Calling a function. Error info is written to the execution log.
Examples of error handling
Case 1: User code goes outside the array boundaries, resulting in ArrayIndexOutOfBoundsException
being thrown. The runtime environment captures the exception and generates a JSON document that contains an error message (the errorMessage
field), error type (the errorType
field), and stack tracestackTrace
field).
Function code:
import java.util.function.Function;
public class Handler implements Function<byte[], Integer> {
@Override
public Integer apply(byte[] input) {
final var array = new int[]{1, 2, 3, 4, 5};
// at this point, the function throws an ArrayIndexOutOfBoundsException
return array[15];
}
}
JSON document returned:
{
"errorMessage": "Index 15 out of bounds for length 5",
"errorType": "ArrayIndexOutOfBoundsException",
"stackTrace": [
...
]
}
Case 2: User code indicates an error by throwing
Function code:
import java.util.function.Function;
public class Handler implements Function<byte[], Integer> {
@Override
public Integer apply(byte[] input) {
throw new RuntimeException("Some error message");
}
}
JSON document returned:
{
"errorMessage": "Some error message",
"errorType": "RuntimeException",
"stackTrace": [
...
]
}