From c3540bfbe19fee66915e48da4d9b697d291629ca Mon Sep 17 00:00:00 2001 From: Alexey Date: Tue, 29 Jul 2025 16:42:25 +0300 Subject: [PATCH] add CatchPanicWithFallback --- internal/core/utils/panic.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/core/utils/panic.go b/internal/core/utils/panic.go index 04f65bf..0d21bda 100644 --- a/internal/core/utils/panic.go +++ b/internal/core/utils/panic.go @@ -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) + } +}