Created
December 18, 2024 15:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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