mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 15:12:26 +00:00
add CatchPanicWithFallback
This commit is contained in:
@@ -39,8 +39,7 @@ func CatchPanic() {
|
||||
}
|
||||
}
|
||||
|
||||
func CatchPanicWithContext(ctx context.Context) {
|
||||
_, cancel := context.WithCancel(ctx)
|
||||
func CatchPanicWithCancel(cancel context.CancelFunc) {
|
||||
if err := recover(); err != nil {
|
||||
stack := make([]byte, 8096)
|
||||
stack = stack[:runtime.Stack(stack, false)]
|
||||
@@ -49,3 +48,13 @@ func CatchPanicWithContext(ctx context.Context) {
|
||||
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