Documentation
¶
Overview ¶
Package panicguard provides panic recovery utilities for Go.
Index ¶
- func Go(fn func())
- func GoCtx(ctx context.Context, fn func(context.Context))
- func GoErr(fn func() error) <-chan error
- func GoNamed(name string, fn func())
- func Middleware(next http.Handler) http.Handler
- func MiddlewareWithHandler(onPanic func(w http.ResponseWriter, r *http.Request, recovered any)) func(http.Handler) http.Handler
- func Recover(r any) error
- func RecoverAs[T error](r any) (T, bool)
- func ResetStats()
- func SetOnPanic(fn func(recovered any, stack []byte))
- type PanicError
- type PanicStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Go ¶
func Go(fn func())
Go runs fn in a new goroutine with panic recovery. If fn panics, the panic is recovered and the global OnPanic handler is called (if set). The panic is not propagated.
func GoCtx ¶ added in v0.2.0
GoCtx runs fn in a new goroutine with panic recovery, passing ctx to fn. If ctx is already cancelled before fn panics, the panic is still recovered.
func GoErr ¶
GoErr runs fn in a new goroutine and returns a buffered channel that will receive the result. If fn returns an error, it is sent on the channel. If fn panics, a *PanicError is sent instead. The channel is closed after the value is sent.
func GoNamed ¶ added in v0.2.0
func GoNamed(name string, fn func())
GoNamed runs fn in a new goroutine with panic recovery. The name is included in the PanicError and passed to the global OnPanic hook for debugging.
func Middleware ¶
Middleware returns an http.Handler that recovers from panics in the next handler. When a panic occurs, it writes a 500 Internal Server Error response and calls the global OnPanic hook if one is set.
func MiddlewareWithHandler ¶
func MiddlewareWithHandler(onPanic func(w http.ResponseWriter, r *http.Request, recovered any)) func(http.Handler) http.Handler
MiddlewareWithHandler returns HTTP middleware that recovers from panics and delegates the response to the provided onPanic function. This allows callers to customize the error response (e.g., returning JSON, logging, etc.).
func Recover ¶
Recover converts a recovered panic value into a *PanicError. It is meant to be used inside a deferred function alongside the built-in recover():
defer func() {
if err := panicguard.Recover(recover()); err != nil {
// handle err (*PanicError)
}
}()
Returns nil if r is nil (no panic occurred).
func RecoverAs ¶ added in v0.2.0
RecoverAs attempts to extract a typed error from a recovered panic value. If r is nil, it returns the zero value of T and false. If r implements T directly, it returns the value and true. Otherwise, r is wrapped in a PanicError and errors.As is used to attempt the conversion.
func ResetStats ¶ added in v0.2.0
func ResetStats()
ResetStats resets the global panic statistics to zero values. This is useful for testing.
func SetOnPanic ¶
SetOnPanic sets a global panic handler that is called whenever a panic is recovered by Go, GoErr, GoNamed, GoCtx, or the HTTP middleware. The handler receives the recovered value and the stack trace captured at the point of the panic. Pass nil to clear the handler.
Types ¶
type PanicError ¶
type PanicError struct {
// Value is the value that was passed to panic().
Value any
// Stack is the raw stack trace captured via runtime/debug.Stack().
Stack []byte
}
PanicError wraps a recovered panic value along with the stack trace captured at the point of the panic.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns a string representation of the panic in the format "panic: {value}".
type PanicStats ¶ added in v0.2.0
type PanicStats struct {
// TotalPanics is the total number of panics recovered across all functions.
TotalPanics int64
// LastPanic is the time of the most recent recovered panic.
LastPanic time.Time
// LastValue is the value from the most recent recovered panic.
LastValue any
}
PanicStats holds global panic statistics.
func Stats ¶ added in v0.2.0
func Stats() PanicStats
Stats returns the current global panic statistics.