/
handler.go
112 lines (95 loc) · 2.44 KB
/
handler.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package e
import (
"context"
"errors"
"log"
"runtime/debug"
"google.golang.org/grpc/codes"
)
type recoverData struct {
shouldReport bool
shouldLog bool
shouldPanic bool
fn func(c context.Context, err *Err)
}
// RecoverOption lets you add additional context to a panic handler
type RecoverOption func(c context.Context, rd *recoverData)
// RecoverNoReport doesn't report the panic
func RecoverNoReport() RecoverOption {
return func(c context.Context, rd *recoverData) {
rd.shouldReport = false
}
}
// RecoverNoLog doesn't log the panic
func RecoverNoLog() RecoverOption {
return func(c context.Context, rd *recoverData) {
rd.shouldLog = false
}
}
// RecoverNoPanic doesn't propagate the panic
func RecoverNoPanic() RecoverOption {
return func(c context.Context, rd *recoverData) {
rd.shouldPanic = false
}
}
// RecoverFunc runs before returning / propagating the panic
// This function provides access to the error created from the panic, save it if you want to use or return it
func RecoverFunc(fn func(c context.Context, err *Err)) RecoverOption {
return func(c context.Context, rd *recoverData) {
rd.fn = fn
}
}
// Recover is intended to be used for recovering from panics. Should only be temporary when
// looking for panics, shouldn't be used long-term in production
// This must be used directly as the panic function so it cannot return an error
// defer e.Recover(c)
func Recover(c context.Context, opts ...RecoverOption) {
rec := recover()
if rec == nil {
return
}
var rawErr error
// find out exactly what the error was and set err
switch x := rec.(type) {
case string:
rawErr = errors.New(x)
case []byte:
rawErr = errors.New(string(x))
case error:
rawErr = x
default:
rawErr = errors.New("unknown panic")
}
rd := &recoverData{
shouldReport: true,
shouldLog: true,
shouldPanic: true,
}
for _, opt := range opts {
opt(c, rd)
}
err := newErr(rawErr, debug.Stack())
err.Level = LevelCritical
err.code = codes.Internal
err.fromHandler = false
if c.Err() != nil {
err = wrap(err, With("c.Err()", c.Err().Error()))
}
if rd.shouldReport {
err.Report(c)
}
if rd.shouldLog {
log.Println(err.Error())
}
// make sure we don't lose any logs due to this panic
// l.Flush(c)
// execute user function
if rd.fn != nil {
// the error with the stack trace is passed in here
// handle the error in your function if you want to use it
rd.fn(c, err)
}
if rd.shouldPanic {
panic(err)
}
}