...
Package grpc_recovery
`grpc_recovery` are interceptors that recover from gRPC handler panics.
Server Side Recovery Middleware
By default a panic will be converted into a gRPC error with `code.Internal`.
Handling can be customised by providing an alternate recovery function.
Please see examples for simple examples of use.
▹ Example (Initialization)
▾ Example (Initialization)
Initialization shows an initialization sequence with a custom recovery handler func.
Code:
package grpc_recovery_test
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
customFunc grpc_recovery.RecoveryHandlerFunc
)
func Example_initialization() {
customFunc = func(p interface{}) (err error) {
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
}
opts := []grpc_recovery.Option{
grpc_recovery.WithRecoveryHandler(customFunc),
}
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_recovery.UnaryServerInterceptor(opts...),
),
grpc_middleware.WithStreamServerChain(
grpc_recovery.StreamServerInterceptor(opts...),
),
)
}
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
StreamServerInterceptor returns a new streaming server interceptor for panic recovery.
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptor for panic recovery.
type Option func(*options)
func WithRecoveryHandler(f RecoveryHandlerFunc) Option
WithRecoveryHandler customizes the function for recovering from a panic.
func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option
WithRecoveryHandlerContext customizes the function for recovering from a panic.
RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
type RecoveryHandlerFunc func(p interface{}) (err error)
RecoveryHandlerFuncContext is a function that recovers from the panic `p` by returning an `error`.
The context can be used to extract request scoped metadata and context values.
type RecoveryHandlerFuncContext func(ctx context.Context, p interface{}) (err error)