Documentation
¶
Overview ¶
Package panicx provides PanicError, a unified error type for recovered panics. It captures the original panic value and the call stack at the point of recovery.
Usage:
defer func() {
if r := recover(); r != nil {
err = panicx.NewPanicError(r)
}
}()
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPanic = errors.New("panic recovered")
ErrPanic is the sentinel for all recovered panics. Use errors.Is to detect, errors.As to access Value and Stack.
Functions ¶
This section is empty.
Types ¶
type PanicError ¶
type PanicError struct {
// Value is the value passed to panic().
Value any
// contains filtered or unexported fields
}
PanicError is created when a recovered panic is converted to an error. It carries the original panic value and the call stack at the point of recovery. Must always be used as a pointer — contains sync.Once, must not be copied.
func NewPanicError ¶
func NewPanicError(r any) *PanicError
NewPanicError creates a PanicError from a recovered panic value. Must be called directly inside the defer func() { if r := recover() } block. The first frame in Stack() is the defer func itself (recover site).
func NewPanicErrorSkip ¶
func NewPanicErrorSkip(r any, skip int) *PanicError
NewPanicErrorSkip is like NewPanicError but skips additional frames. skip=0 identifies the caller of NewPanicErrorSkip. Add 1 per extra wrapping layer between the defer and this call.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error implements the error interface.
func (*PanicError) Is ¶
func (e *PanicError) Is(target error) bool
Is reports whether this error matches target. Supports errors.Is(err, panicx.ErrPanic).
func (*PanicError) Stack ¶
func (e *PanicError) Stack() []string
Stack returns the formatted call stack at the point of the panic. Each element is one frame: "func.Name\n\tfile.go:line". Computed lazily on first call and cached. Safe for concurrent use. The last frame (runtime.main/runtime.goexit) is omitted.