Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion recovery/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE for licensing terms.

/*
`grpc_recovery` are intereceptors that recover from gRPC handler panics.
`grpc_recovery` are interceptors that recover from gRPC handler panics.

Server Side Recovery Middleware

Expand Down
9 changes: 5 additions & 4 deletions recovery/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package grpc_recovery

import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc"
)

// RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
Expand All @@ -22,7 +23,7 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ interface{}, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverFrom(ctx, r, o.recoveryHandlerFunc)
err = recoverFrom(ctx, r, o.recoveryHandlerFuncContext)
}
}()

Expand All @@ -36,7 +37,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverFrom(stream.Context(), r, o.recoveryHandlerFunc)
err = recoverFrom(stream.Context(), r, o.recoveryHandlerFuncContext)
}
}()

Expand All @@ -46,7 +47,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {

func recoverFrom(ctx context.Context, p interface{}, r RecoveryHandlerFuncContext) error {
if r == nil {
return grpc.Errorf(codes.Internal, "%s", p)
return status.Errorf(codes.Internal, "%s", p)
}
return r(ctx, p)
}
8 changes: 4 additions & 4 deletions recovery/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import "golang.org/x/net/context"

var (
defaultOptions = &options{
recoveryHandlerFunc: nil,
recoveryHandlerFuncContext: nil,
}
)

type options struct {
recoveryHandlerFunc RecoveryHandlerFuncContext
recoveryHandlerFuncContext RecoveryHandlerFuncContext
}

func evaluateOptions(opts []Option) *options {
Expand All @@ -29,7 +29,7 @@ type Option func(*options)
// WithRecoveryHandler customizes the function for recovering from a panic.
func WithRecoveryHandler(f RecoveryHandlerFunc) Option {
return func(o *options) {
o.recoveryHandlerFunc = RecoveryHandlerFuncContext(func(ctx context.Context, p interface{}) error {
o.recoveryHandlerFuncContext = RecoveryHandlerFuncContext(func(ctx context.Context, p interface{}) error {
return f(p)
})
}
Expand All @@ -38,6 +38,6 @@ func WithRecoveryHandler(f RecoveryHandlerFunc) Option {
// WithRecoveryHandlerContext customizes the function for recovering from a panic.
func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option {
return func(o *options) {
o.recoveryHandlerFunc = f
o.recoveryHandlerFuncContext = f
}
}