panicx

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 4 Imported by: 0

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

View Source
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL