From 94f5ccf378facc352ad2ff9f436cbbc4dab142fc Mon Sep 17 00:00:00 2001 From: Shu Xian Date: Wed, 11 Sep 2019 00:40:40 +0800 Subject: [PATCH] recover handler if panic --- grpc.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/grpc.go b/grpc.go index 4dcd6d7..8da88b9 100644 --- a/grpc.go +++ b/grpc.go @@ -338,6 +338,11 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service, // define the handler func fn := func(ctx context.Context, req server.Request, rsp interface{}) error { + defer func() { + if r := recover(); r != nil { + log.Logf("handler %s panic recovered, err: %s", mtype.method.Name, r) + } + }() returnValues = function.Call([]reflect.Value{service.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)}) // The return value for the method is an error.