C# function error handling
If a handler reports a C# 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.
The error data is also written to the execution log. You can view it using the Yandex Cloud CLI or the management console
Examples of error handling
Case 1: user code goes outside the array boundaries, an IndexOutOfRangeException
is 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:
public class Handler {
public int FunctionHandler(byte[] input) {
readonly var array = new int[]{1, 2, 3, 4, 5};
// at this point, the function throws an IndexOutOfRangeException
return array[15];
}
}
JSON document returned:
{
"errorMessage": "Index was outside the bounds of the array",
"errorType": "System.IndexOutOfRangeException",
"stackTrace": [
...
]
}
Case 2: user code indicates an error by throwing
Function code:
public class Handler {
public int FunctionHandler(byte[] input) {
throw new SystemException("Some error message");
}
}
JSON document returned:
{
"errorMessage": "Some error message",
"errorType": "System.SystemException",
"stackTrace": [
...
]
}