Skip to content

A recoverable errgroup based on `x/sync/errgroup` that can recover from panics. Panics are caught and re-panicked in the Wait function.

License

Notifications You must be signed in to change notification settings

min0625/errgroup

Repository files navigation

errgroup

Go Reference

A recoverable errgroup based on golang.org/x/sync/errgroup that can recover from panics. Panics are caught and re-panicked in the Wait function.

Ref: golang/go#53757

Installation

go get github.com/min0625/errgroup

Example

func Example() {
	// This case uses "github.com/min0625/errgroup" which will catch panics.
	// If you import "golang.org/x/sync/errgroup" instead, it won't catch panics.
	// You can try this in the Go Playground: https://go.dev/play/p/7pUX6uQ2mCH
	var g errgroup.Group

	defer func() {
		// Will catch the panic.
		if p := recover(); p != nil {
			switch t := p.(type) {
			case errgroup.PanicValue:
				fmt.Println(t.Recovered)
			case errgroup.PanicError:
				fmt.Println(t.Recovered)
			}
		}
	}()

	g.Go(func() error {
		// Do something
		return nil
	})

	g.Go(func() error {
		panic("oops")
	})

	if err := g.Wait(); err != nil {
		// Handle error
	}

	// Output: oops
}

About

A recoverable errgroup based on `x/sync/errgroup` that can recover from panics. Panics are caught and re-panicked in the Wait function.

Resources

License

Stars

Watchers

Forks

Packages

No packages published