1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package panichandler
import (
"context"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
_ grpc.UnaryServerInterceptor = UnaryPanicHandler
_ grpc.StreamServerInterceptor = StreamPanicHandler
)
// PanicHandler is a handler that will be called on a grpc panic
type PanicHandler func(methodName string, error interface{})
func toPanicError(grpcMethodName string, r interface{}) error {
return status.Errorf(codes.Internal, "panic: %v", r)
}
// UnaryPanicHandler handles request-response panics
func UnaryPanicHandler(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer handleCrash(info.FullMethod, func(grpcMethodName string, r interface{}) {
err = toPanicError(grpcMethodName, r)
})
return handler(ctx, req)
}
// StreamPanicHandler handles stream panics
func StreamPanicHandler(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
defer handleCrash(info.FullMethod, func(grpcMethodName string, r interface{}) {
err = toPanicError(grpcMethodName, r)
})
return handler(srv, stream)
}
var additionalHandlers []PanicHandler
// InstallPanicHandler installs additional crash handles for dealing with a panic
func InstallPanicHandler(handler PanicHandler) {
additionalHandlers = append(additionalHandlers, handler)
}
func handleCrash(grpcMethodName string, handler PanicHandler) {
if r := recover(); r != nil {
log.WithFields(log.Fields{
"error": r,
"method": grpcMethodName,
}).Error("grpc panic")
handler(grpcMethodName, r)
for _, fn := range additionalHandlers {
fn(grpcMethodName, r)
}
}
}
|