github.com/prysmaticlabs/prysm@v1.4.4/shared/traceutil/recovery_interceptor_option.go (about)

     1  package traceutil
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"runtime"
     8  	"runtime/debug"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"go.opencensus.io/trace"
    12  )
    13  
    14  // RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
    15  // The context can be used to extract request scoped metadata and context values.
    16  func RecoveryHandlerFunc(ctx context.Context, p interface{}) error {
    17  	span := trace.FromContext(ctx)
    18  	if span != nil {
    19  		span.AddAttributes(trace.StringAttribute("stack", string(debug.Stack())))
    20  	}
    21  	var err error
    22  	switch v := p.(type) {
    23  	case runtime.Error:
    24  		err = errors.New(v.Error())
    25  	default:
    26  		err = fmt.Errorf("%v", p)
    27  	}
    28  
    29  	logrus.WithError(err).WithField("stack", string(debug.Stack())).Error("gRPC panicked!")
    30  	return err
    31  }