-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
36 lines (29 loc) · 857 Bytes
/
main.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
package main
import (
"fmt"
"github.com/id27182/gomodpanichandler/anothermodule"
"github.com/id27182/gomodpanichandler/module"
"runtime/debug"
"strings"
)
func main() {
defer handlePanicFromModule("module")
// panic from this module will be handled.
// comment and rerun to see that panics from anothermodule will not be handled
module.CausePanic()
// panic from this module will not be handled
anothermodule.CausePanic()
}
func handlePanicFromModule(moduleName string) {
if e := recover(); e != nil {
// getting a call stack
stackBytes := debug.Stack()
stackString := string(stackBytes)
// dummy way to check if call stack contains module name just to show that it's possible
if strings.Contains(stackString, fmt.Sprintf("/%s/", moduleName)) {
fmt.Printf("handling panic in %s", moduleName)
} else {
panic(e)
}
}
}