diff --git a/grpc.go b/grpc.go index 8461aca..e03c6ef 100644 --- a/grpc.go +++ b/grpc.go @@ -197,31 +197,6 @@ func (g *grpcServer) getGrpcOptions() []grpc.ServerOption { } func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err error) { - defer func() { - if r := recover(); r != nil { - g.RLock() - config := g.opts - g.RUnlock() - if config.Logger.V(logger.ErrorLevel) { - config.Logger.Error(config.Context, "panic recovered: ", r) - config.Logger.Error(config.Context, string(debug.Stack())) - } - err = errors.InternalServerError(g.opts.Name, "panic recovered: %v", r) - } else if err != nil { - g.RLock() - config := g.opts - g.RUnlock() - if config.Logger.V(logger.ErrorLevel) { - config.Logger.Errorf(config.Context, "grpc handler got error: %s", err) - } - } - }() - - if g.wg != nil { - g.wg.Add(1) - defer g.wg.Done() - } - fullMethod, ok := grpc.MethodFromServerStream(stream) if !ok { return status.Errorf(codes.Internal, "method does not exist in context") @@ -232,6 +207,31 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err err return status.New(codes.InvalidArgument, err.Error()).Err() } + defer func() { + if r := recover(); r != nil { + g.RLock() + config := g.opts + g.RUnlock() + if config.Logger.V(logger.ErrorLevel) { + config.Logger.Errorf(config.Context, "panic in %s.%s recovered: %v", serviceName, methodName, r) + config.Logger.Error(config.Context, string(debug.Stack())) + } + err = errors.InternalServerError(g.opts.Name, "panic in %s.%s recovered: %v", serviceName, methodName, r) + } else if err != nil { + g.RLock() + config := g.opts + g.RUnlock() + if config.Logger.V(logger.ErrorLevel) { + config.Logger.Errorf(config.Context, "grpc handler %s.%s got error: %s", serviceName, methodName, err) + } + } + }() + + if g.wg != nil { + g.wg.Add(1) + defer g.wg.Done() + } + // get grpc metadata gmd, ok := gmetadata.FromIncomingContext(stream.Context()) if !ok {