package grpc_recovery
import "github.com/grpc-ecosystem/go-grpc-middleware/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.
Initialization shows an initialization sequence with a custom recovery handler func.
Code:play
Example (Initialization)¶
package main
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
)
// Initialization shows an initialization sequence with a custom recovery handler func.
func main() {
// Define customfunc to handle panic
customFunc = func(p interface{}) (err error) {
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
}
// Shared options for the logger, with a custom gRPC code to log level function.
opts := []grpc_recovery.Option{
grpc_recovery.WithRecoveryHandler(customFunc),
}
// Create a server. Recovery handlers should typically be last in the chain so that other middleware
// (e.g. logging) can operate on the recovered state instead of being directly affected by any panic
_ = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_recovery.UnaryServerInterceptor(opts...),
),
grpc_middleware.WithStreamServerChain(
grpc_recovery.StreamServerInterceptor(opts...),
),
)
}
Index ¶
- func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
- func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
- type Option
- func WithRecoveryHandler(f RecoveryHandlerFunc) Option
- func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option
- type RecoveryHandlerFunc
- type RecoveryHandlerFuncContext
Examples ¶
Functions ¶
func StreamServerInterceptor ¶
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
StreamServerInterceptor returns a new streaming server interceptor for panic recovery.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a new unary server interceptor for panic recovery.
Types ¶
type Option ¶
type Option func(*options)
func WithRecoveryHandler ¶
func WithRecoveryHandler(f RecoveryHandlerFunc) Option
WithRecoveryHandler customizes the function for recovering from a panic.
func WithRecoveryHandlerContext ¶
func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option
WithRecoveryHandlerContext customizes the function for recovering from a panic.
type RecoveryHandlerFunc ¶
type RecoveryHandlerFunc func(p interface{}) (err error)
RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
type RecoveryHandlerFuncContext ¶
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.
Source Files ¶
doc.go interceptors.go options.go
- Version
- v1.3.0
- Published
- Apr 22, 2021
- Platform
- js/wasm
- Imports
- 4 packages
- Last checked
- 2 minutes ago –
Tools for package owners.