safe

package
v0.0.2-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(fn func())

Go starts a new goroutine that runs the provided function with panic recovery. Any panic in the function will be caught and logged without crashing the program.

func IfErrorPresent

func IfErrorPresent(fn func() error)

IfErrorPresent executes the given function and handles any error using the configured error handler. This is useful for safely executing functions that may fail without disrupting the main flow.

func IfErrorXPresent

func IfErrorXPresent[T any](fn func() (T, error))

IfErrorXPresent executes the given function that returns a value and error, handling any error. The returned value is discarded, making this useful for operations where you only care about side effects.

func InitErrorHandler

func InitErrorHandler(handler ErrorHandler)

InitErrorHandler sets a custom error handler for the safe package. If handler is nil, the default error handler remains unchanged.

func Recover

func Recover(onError ...func(err any))

Recover handles panics and logs them with optional custom error handlers. It should be used in defer statements to catch and handle panics gracefully. Optional onError functions are called with the panic value for custom handling.

func Run

func Run(fn func())

Run executes a function with panic recovery protection. It can be used to wrap potentially panicking code with automatic recovery.

Types

type ErrorHandler

type ErrorHandler func(error)

ErrorHandler defines a function type for handling errors with contextual labels. It takes a descriptive label and the error that occurred.

Jump to

Keyboard shortcuts

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