Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created December 18, 2024 15:43
Show Gist options
  • Save timruffles/62a73b11b7ee8ade3cc3793c4f685432 to your computer and use it in GitHub Desktop.
Save timruffles/62a73b11b7ee8ade3cc3793c4f685432 to your computer and use it in GitHub Desktop.
FromPanicValue - returns an error with the best possible stack-trace from the value obtained from calling recover() within a defer
package error
import pkgerrors "github.com/pkg/errors"
// FromPanicValue returns an error with the best possible stack-trace from
// the value obtained from calling recover() within a defer
func FromPanicValue(pv any) error {
if pv == nil {
return nil
}
err, ok := pv.(error)
if !ok {
return pkgerrors.Errorf("panic with value: %+v", pv)
}
if _, ok := err.(interface {
StackTrace() pkgerrors.StackTrace
}); ok {
return err
}
// capture a stack if we're missing one
return pkgerrors.WithStack(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment