Go function error handling
If a handler reports a Go 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, a panic
is expected. In fact, the handler captures the panic
automatically, aggregates information about it, and generates a JSON document that contains an error message (the errorMessage
field), error type (the errorType
field), and stack tracestackTrace
field).
Function code:
package main
func Handler () (int, error) {
array := []int{1, 2, 3, 4, 5}
return array[15], nil
}
JSON document returned:
{
"errorMessage": "user code execution causes panic: runtime error: index out of range [15] with length 5",
"errorType": "UserCodePanic",
"stackTrace": [
...
]
}
Case 2: user code reports an error by returning it from the function. In this case, the handler captures this error and generates a JSON document with a user-defined error message (the errorMessage
field) and its type (the errorType
field).
Function code:
package main
import "fmt"
func Handler () (int, error) {
return 0, fmt.Errorf("an error")
}
JSON document returned:
{
"errorMessage": "an error",
"errorType": "UserCodeError"
}