This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panichandler.go
67 lines (56 loc) · 1.64 KB
/
panichandler.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
package panichandler
import (
"unsafe"
"sync"
"github.com/agiledragon/gomonkey/v2"
)
//go:linkname fatalpanic runtime.fatalpanic
func fatalpanic(msgs *_panic)
//nolint:structcheck
//go:linkname _panic runtime._panic
type _panic struct {
argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink
arg interface{} // argument to panic
link *_panic // link to earlier panic
recovered bool // whether this panic is over
aborted bool // the panic was aborted
}
// Handler will be put on the panic handling stack
//
// the first parameter will contain the variable passed into panic()
//
// what ever you will return will be handed over to the next panic() handler up until the final panic
//
// if you return an instance of IgnorePanic{} the panic will be ignored and execution continues
// note that this will mostly result in more panics
type Handler func(interface{}) interface{}
var handlers []Handler
var mu sync.Mutex
// IgnorePanic can be returned in the Handler to ignore the panic and continue execution
type IgnorePanic struct{}
func init() {
patch()
}
func patch() {
var patches *gomonkey.Patches
patches = gomonkey.ApplyFunc(fatalpanic, func(v *_panic) {
patches.Reset()
defer patch()
mu.Lock()
defer mu.Unlock()
for _, handler := range handlers {
if v.arg = handler(v.arg); v.arg != nil {
if _, ok := v.arg.(IgnorePanic); ok {
return
}
}
}
fatalpanic(v)
})
}
// OnPanic adds the specified handler function to the panic handler stack
func OnPanic(fn Handler) {
mu.Lock()
handlers = append(handlers, fn)
mu.Unlock()
}