When a function fails in the Java runtime, Function Compute captures the error and returns an error response. The structure of the response depends on how the failure occurred.
Error scenarios
Unhandled exception
If the function throws an uncaught exception, Function Compute captures it and returns a structured error response that includes the exception type and stack trace.
The following example throws an IOException:
package example;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class HelloFC implements StreamRequestHandler {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
throw new IOException("oops");
}
}The invocation returns:
{
"errorMessage" : "oops",
"errorType" : "java.io.IOException",
"errorCause" : "oops",
"stackTrace" : [ "example.HelloFC.handleRequest(HelloFC.java:15)" ]
}Unexpected process exit
If the function proactively exits during running, Function Compute returns a general error message.
The following example calls System.exit(-1):
package example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;
public class App implements StreamRequestHandler {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
System.exit(-1);
}
}The invocation returns:
{
"errorMessage": "Process exited unexpectedly before completing request (duration: 43ms, maxMemoryUsage: 65MB)"
}Error response fields
The following fields may appear in an error response.
| Field | Type | Description |
|---|---|---|
errorMessage | String | The error message. |
errorType | String | The error type. |
stackTrace | List | The stack trace of the error. |
For more information, see Basics.