add CatchPanicWithFallback

This commit is contained in:
2025-07-29 16:42:25 +03:00
parent bd54628b5c
commit c3540bfbe1

View File

@@ -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)
}
}