Documentation ¶
Overview ¶
Package safe provides helpers for gracefully handling panics in background goroutines.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do executes fn. If a panic occurs, it will be recovered and returned as a safe.PanicError.
Example ¶
getName := func() string { panic("unhandled error") } err := safe.Do(func() error { _ = getName() return nil }) if err, ok := err.(safe.PanicError); ok { fmt.Println(err) }
Output: panic: unhandled error
func DoWithResult ¶
DoWithResult executes fn. If a panic occurs, it will be recovered and returned as a safe.PanicError.
Example ¶
getName := func() string { panic("unhandled error") } _, err := safe.DoWithResult(func() (interface{}, error) { _ = getName() return nil, nil }) if err, ok := err.(safe.PanicError); ok { fmt.Println(err) }
Output: panic: unhandled error
Example (Ok) ¶
getName := func() string { return "OK" } res, err := safe.DoWithResult(func() (interface{}, error) { return getName(), nil }) if err, ok := err.(safe.PanicError); ok { fmt.Println(err) } else { fmt.Println(res.(string)) }
Output: OK
func Go ¶
func Go(fn func())
Go executes fn in a background goroutine. If a panic occurs, it will be recovered and passed to the global panic handler.
Example ¶
safe.SetPanicHandler(func(err error) { fmt.Println(err) }) var wg sync.WaitGroup wg.Add(1) safe.Go(func() { defer wg.Done() panic("unhandled error") }) wg.Wait()
Output: panic: unhandled error
func SetPanicHandler ¶
func SetPanicHandler(fn func(err error))
SetPanicHandler configures a global handler for any panics that occur in background goroutines spawned by safe.Go. If unset, they'll instead be written directly to the log.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
A Group is a drop-in replacement for errgroup.Group, a collection of goroutines working on subtasks that are part of the same overall task. If any panics occur, they will be recovered and returned as a safe.PanicError.
A zero Group is valid and does not cancel on error.
Example ¶
var g safe.Group g.Go(func() error { panic("unhandled error") }) err := g.Wait() fmt.Printf("(%T) %v\n", err, err)
Output: (safe.PanicError) panic: unhandled error
func GroupWithContext ¶
GroupWithContext returns a new Group and an associated Context derived from ctx.
The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.
func (*Group) Go ¶
Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.
The first call to panic or return a non-nil error cancels the group's context, if the group was created by calling WithContext. The error will be returned by Wait.
func (*Group) SetLimit ¶
SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit.
Any subsequent call to the Go method will block until it can add an active goroutine without exceeding the configured limit.
The limit must not be modified while any goroutines in the group are active.
type PanicError ¶
type PanicError struct {
// contains filtered or unexported fields
}
PanicError is an error that wraps a panic value. It also embeds a pkg/errors error to ensure the stack trace is properly captured and rendered to any error reporters.
func (PanicError) Panic ¶
func (p PanicError) Panic() interface{}
Panic returns the underlying value passed to panic().