diff options
Diffstat (limited to 'internal/util/fns.go')
| -rw-r--r-- | internal/util/fns.go | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/internal/util/fns.go b/internal/util/fns.go index 53f5ebeed..c82377030 100644 --- a/internal/util/fns.go +++ b/internal/util/fns.go @@ -32,18 +32,7 @@ func Must(fn func()) { panic("nil func") } for !func() (done bool) { - defer func() { - if r := recover(); r != nil { - // Gather calling func frames. - pcs := make([]uintptr, 10) - n := runtime.Callers(3, pcs) - i := runtime.CallersFrames(pcs[:n]) - c := gatherFrames(i, n) - - const msg = "recovered panic: %v\n\n%s\n" - fmt.Fprintf(os.Stderr, msg, r, c.String()) - } - }() + defer Recover() fn() done = true return @@ -51,6 +40,21 @@ func Must(fn func()) { } } +// Recover wraps runtime.recover() to dump the current +// stack to stderr on panic and return the panic value. +func Recover() any { + if r := recover(); r != nil { + // Gather calling func frames. + pcs := make([]uintptr, 10) + n := runtime.Callers(3, pcs) + i := runtime.CallersFrames(pcs[:n]) + c := gatherFrames(i, n) + fmt.Fprintf(os.Stderr, "recovered panic: %v\n\n%s\n", r, c.String()) + return r + } + return nil +} + // gatherFrames collates runtime frames from a frame iterator. func gatherFrames(iter *runtime.Frames, n int) errors.Callers { if iter == nil { |
