panicx

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package panicx provides panic recovery utilities for goroutine safety. It ensures that panics in spawned goroutines are caught and logged rather than crashing the entire process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Recover

func Recover(logger *slog.Logger, context string)

Recover is a defer-friendly panic recovery function. It logs the panic with full stack trace. Use it like:

defer panicx.Recover(logger, "myGoroutine")

func RecoverWithCallback

func RecoverWithCallback(logger *slog.Logger, context string, callback func(panicValue any, stack []byte))

RecoverWithCallback recovers from panic and calls the provided callback. The callback receives the panic value and stack trace.

func SafeGo

func SafeGo(logger *slog.Logger, fn func())

SafeGo launches a goroutine with panic recovery. The panic is logged with full stack trace, and the process continues.

func SafeGoWithContext

func SafeGoWithContext(ctx context.Context, logger *slog.Logger, fn func(context.Context))

SafeGoWithContext launches a goroutine with context and panic recovery. The context is passed to the function for cancellation support.

func SafeGoWithPolicy

func SafeGoWithPolicy(logger *slog.Logger, policy RecoveryPolicy, context string, fn func())

SafeGoWithPolicy launches a goroutine with a specific recovery policy. The policy determines what happens after a panic is recovered.

Types

type RecoveryPolicy

type RecoveryPolicy int

RecoveryPolicy defines how to handle recovered panics.

const (
	// PolicyLogAndContinue logs the panic and continues normal operation.
	// This is the default policy for non-critical goroutines.
	PolicyLogAndContinue RecoveryPolicy = iota

	// PolicyLogAndRestart logs the panic and signals for component restart.
	// Use for goroutines that should be restarted after failure.
	PolicyLogAndRestart

	// PolicyLogAndShutdown logs the panic and triggers graceful shutdown.
	// Use for critical goroutines where continued operation is unsafe.
	PolicyLogAndShutdown
)

Jump to

Keyboard shortcuts

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