multierrgroup

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: MIT Imports: 3 Imported by: 5

README

multierrgroup

Go Reference Github Workflow codecov

A simple combination of golang.org/x/sync/errgroup and go.uber.org/multierr.

multierrgroup have the exact same behaviour as errgroup, except that it captures all errors returned from the goroutines instead of just one.

Usage

import "go.ptx.dk/multierrgroup"

multierrgroup is a drop-in replacement for errgroup and have the same methods and behaviour.

When any goroutine returns an error, it will colleceted using multierr.Append and Wait will return the multierr. Thus error can be split into their original errors, or worked on using errors.As/Is.

See the multierr documentation for more information.

Sample

package main

import (
	"errors"
	"fmt"
	"os"

	"go.ptx.dk/multierrgroup"
	"go.uber.org/multierr"
)

func main() {
	g := multierrgroup.Group{}

	for i := 0; i < 10; i++ {
		str := fmt.Sprintf("Error: %d", i)
		g.Go(func() error {
			return errors.New(str)
		})
		if i == 5 {
			g.Go(func() error {
				return os.ErrNotExist
			})
		}
	}
	err := g.Wait()
	fmt.Println("Got", len(multierr.Errors(err)), "errors")
	fmt.Println(err.Error())
	fmt.Println("Was one of the errors a ErrNotExists?", errors.Is(err, os.ErrNotExist))
}

Running the sample shows that errors from all goroutines are kept:

$ go run thesolution/main.go
Got 11 errors
Error: 9; Error: 5; file does not exist; Error: 6; Error: 7; Error: 8; Error: 1; Error: 0; Error: 2; Error: 3; Error: 4
Was one of the errors a ErrNotExists? true

Documentation

Overview

Package multierrgroup provides a mix of multierr and errgroup See documentation for https://pkg.go.dev/go.uber.org/multierr and https://pkg.go.dev/golang.org/x/sync/errgroup

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group manages goroutines and collect all the errors. See https://pkg.go.dev/golang.org/x/sync/errgroup#Group for more information

Example
package main

import (
	"errors"
	"fmt"

	"go.ptx.dk/multierrgroup"
	"go.uber.org/multierr"
)

func main() {
	g := multierrgroup.Group{}
	g.Go(func() error {
		return errors.New("error 1")
	})
	g.Go(func() error {
		return errors.New("error 2")
	})
	err := g.Wait()
	// Using golang.org/x/sync/errgroup would return a return error depending on which goroutine was scheduled first

	errs := multierr.Errors(err)
	fmt.Println("Got", len(errs), "errors")
}
Output:

Got 2 errors

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new Group with a associated Context. The context will be canceled if any goroutine returns an error. See https://pkg.go.dev/golang.org/x/sync/errgroup#WithContext

func (*Group) Go

func (g *Group) Go(f func() error)

Go calls the function in a new goroutine. If a non-nil errors is returned, the context is canceled and the error is collected using multierr and will be returned by Wait.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all goroutines have completed.

All errors returned from the goroutines will be combined into one using multierr and returned from this method.

Directories

Path Synopsis
samples

Jump to

Keyboard shortcuts

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