in case of panic, return full service handler name #130

Merged
vtolstov merged 1 commits from panic_info into v3 2022-11-02 23:12:27 +03:00
Showing only changes of commit 84b1b862a7 - Show all commits

50
grpc.go
View File

@ -197,31 +197,6 @@ func (g *grpcServer) getGrpcOptions() []grpc.ServerOption {
} }
func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) (err error) { 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) fullMethod, ok := grpc.MethodFromServerStream(stream)
if !ok { if !ok {
return status.Errorf(codes.Internal, "method does not exist in context") 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() 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 // get grpc metadata
gmd, ok := gmetadata.FromIncomingContext(stream.Context()) gmd, ok := gmetadata.FromIncomingContext(stream.Context())
if !ok { if !ok {