mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 19:32:26 +00:00
add CatchPanicWithFallback
This commit is contained in:
@@ -39,8 +39,7 @@ func CatchPanic() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CatchPanicWithContext(ctx context.Context) {
|
func CatchPanicWithCancel(cancel context.CancelFunc) {
|
||||||
_, cancel := context.WithCancel(ctx)
|
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
stack := make([]byte, 8096)
|
stack := make([]byte, 8096)
|
||||||
stack = stack[:runtime.Stack(stack, false)]
|
stack = stack[:runtime.Stack(stack, false)]
|
||||||
@@ -49,3 +48,13 @@ func CatchPanicWithContext(ctx context.Context) {
|
|||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CatchPanicWithFallback(onPanic func(any)) {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
stack := make([]byte, 8096)
|
||||||
|
stack = stack[:runtime.Stack(stack, false)]
|
||||||
|
stack = trimStackPaths(stack, "GoSally-mvp")
|
||||||
|
log.Printf("recovered panic:\n%s", stack)
|
||||||
|
onPanic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user