diff --git a/recovery/doc.go b/recovery/doc.go index da40190c5..2806de5e2 100644 --- a/recovery/doc.go +++ b/recovery/doc.go @@ -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 diff --git a/recovery/interceptors.go b/recovery/interceptors.go index f687e2ba8..79abf149b 100644 --- a/recovery/interceptors.go +++ b/recovery/interceptors.go @@ -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`. @@ -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) } }() @@ -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) } }() @@ -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) } diff --git a/recovery/options.go b/recovery/options.go index 07bacda3a..3845300a0 100644 --- a/recovery/options.go +++ b/recovery/options.go @@ -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 { @@ -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) }) } @@ -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 } }