Documentation ¶
Overview ¶
Package errgroup provides utility functions and types for easily recovering from panics.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrRecover ¶
func ErrRecover(errI interface{}) error
ErrRecover converts a recover() call into an error. Prefer using WrapEgGoWithRecover.
The caller must call recover(), because the stack changes when ErrRecover is called.
See https://play.golang.org/p/FYD2QlmHHO9
Usage
defer func() { err = helpers.ErrRecover(recover()) }()
Example (ErrorWrapping) ¶
ErrorWrapping illustrates how ErrRecover can be used to recover from a panic and convert it into a useful error instead. This allows the caller of the panicking function to regain control and handle the panic as desired.
WrapEgGoWithRecover is simply a wrapper to avoid needing to manually do this wrapping.
package main import ( "errors" "fmt" "github.com/getkalido/panicgroup" ) var ErrSomethingTerrible error = errors.New("something terrible has happened!") func main() { // An example function, which has a chance to panic. panicFunc := func() (err error) { defer func() { // ErrRecover should recover from the panic and convert it to // an error. errR := panicgroup.ErrRecover(recover()) if errR != nil { err = errR } }() panic(ErrSomethingTerrible) } err := panicFunc() if err == nil { fmt.Println("No error was captured.") } else { fmt.Println("The panic was successfully converted into an error.") } }
Output: The panic was successfully converted into an error.
func WrapEgGoWithRecover ¶
WrapEgGoWithRecover takes an input function f and wraps it in panic recovery code. If a panic occurs, it is captured and instead converted to a regular error.
Example (SimplifiedWrapping) ¶
SimplifiedWrapping illustrates how WrapEgGoWithRecover can be used to recover from a panic and convert it into a useful error instead. This allows the caller of the panicking function to regain control and handle the panic as desired.
This simplifies the manual process of adding panic recovery to a block of code.
package main import ( "errors" "fmt" "github.com/getkalido/panicgroup" ) var ErrSomethingTerrible error = errors.New("something terrible has happened!") func main() { // An example function, which has a chance to panic. panicFunc := panicgroup.WrapEgGoWithRecover(func() (err error) { panic(ErrSomethingTerrible) }) err := panicFunc() if err == nil { fmt.Println("No error was captured.") } else { fmt.Println("The panic was successfully converted into an error.") } }
Output: The panic was successfully converted into an error.
Types ¶
type WgErrGroup ¶
WgErrGroup re-implements the core functionality of errgroup.Group, but wrapped in a panic-handling function.
func WaitGroup ¶
func WaitGroup() WgErrGroup
WaitGroup returns a new instance of WgErrGroup.
WaitGroupWithContext should be preferred, since it is able to cancel execution if one of the goroutines started with Go encounters an error.
func WaitGroupWithContext ¶
func WaitGroupWithContext(ctx context.Context) (WgErrGroup, context.Context)
WaitGroupWithContext returns a new WgErrGroup 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.