forked from wothing/worpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery.go
33 lines (26 loc) · 855 Bytes
/
recovery.go
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
// Copyright (c) 2016 Wothing Co., Ltd. All rights reserved.
package worpc
import (
"runtime"
"github.com/wothing/log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
MAXSTACKSIZE = 4096
)
func Recovery(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// recovery func
defer func() {
if r := recover(); r != nil {
// log stack
stack := make([]byte, MAXSTACKSIZE)
stack = stack[:runtime.Stack(stack, false)]
log.Terrorf(GetTidFromContext(ctx), "panic grpc invoke: %s, err=%v, stack:\n%s", info.FullMethod, r, string(stack))
// if panic, set custom error to 'err', in order that client and sense it.
err = grpc.Errorf(codes.Unknown, "internal panic error: %v", r)
}
}()
return handler(ctx, req)
}