Documentation ¶
Overview ¶
The multierror package provides useful helpers to handle multiple errors wrapped together.
This package predates the errors.Join feature available in the stdlib since 1.20. For backwards compatibility I left the functionality that overlaps with the stdlib but marked is a depreacted.
This package offers a few interesting functions that help with joined errors, namely formatters and grouping.
Index ¶
- Variables
- func Append(err error, errs ...error) errordeprecated
- func Fold(errs []error) error
- func Format(err error, f Formatter) error
- func InlineFormatter(errs []string) string
- func Join(errs []error, opts ...JoinOption) errordeprecated
- func Split(err error) []error
- func Tag(tag string, err error) error
- func TaggedError(err error) (string, string)
- func Transform(err error, fn func([]error) []error) error
- func Unfold(err error) []errordeprecated
- func Uniq(errs []error) []error
- type Error
- type Formatter
- type JoinOption
- type TaggableError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Append
deprecated
Deprecated: Append is deprecated. Use errors.Join instead
Append creates a new mutlierror.Error structure or appends the arguments to an existing multierror err can be nil, or can be a non-multierror error.
If err is nil and errs has only one element, that element is returned. I.e. a singleton error is never treated and (thus rendered) as a multierror. This also also effectively allows users to just pipe through the error value of a function call, without having to first check whether the error is non-nil.
Example ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var errs error errs = multierror.Append(errs, fmt.Errorf("foo")) errs = multierror.Append(errs, fmt.Errorf("bar")) errs = multierror.Append(errs, fmt.Errorf("baz")) fmt.Printf("%v", errs) }
Output: 3 errors occurred: foo bar baz
func Fold ¶ added in v0.2.0
deprecated: Fold is deprecated, use Join instead.
Fold turns a slice of errors into a multierror.
func InlineFormatter ¶ added in v0.2.0
InlineFormatter formats all errors in a single line.
func Join
deprecated
added in
v0.3.0
func Join(errs []error, opts ...JoinOption) error
Deprecated: Join is deprecated. Use errors.Join
Join turns a slice of errors into a multierror.
Example ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var errs []error errs = append(errs, fmt.Errorf("foo")) errs = append(errs, fmt.Errorf("bar")) errs = append(errs, fmt.Errorf("foo")) err := multierror.Join(errs) fmt.Printf("%v", err) }
Output: 3 errors occurred: foo bar foo
func Split ¶ added in v0.3.0
Split returns the underlying list of errors wrapped in a multierror.
A multierror is an error tha implements Unwrap() []error (see https://pkg.golang.ir/errors) Errors produced by multierror.Join implement Unwrap() []error.
Example ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var err error err = multierror.Append(err, fmt.Errorf("foo")) err = multierror.Append(err, fmt.Errorf("bar")) err = multierror.Append(err, fmt.Errorf("baz")) fmt.Printf("%q", multierror.Split(err)) }
Output: ["foo" "bar" "baz"]
func Tag ¶ added in v0.2.0
Tag wraps an error with a tag. The resulting error implements the TaggableError interface and thus the tags can be unwrapped by Uniq in order to deduplicate error messages without loosing context.
Example ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var err error err = multierror.Append(err, multierror.Tag("k1", fmt.Errorf("foo"))) err = multierror.Append(err, multierror.Tag("k2", fmt.Errorf("foo"))) err = multierror.Append(err, multierror.Tag("k3", fmt.Errorf("bar"))) fmt.Printf("%v", err) }
Output: 3 errors occurred: foo (k1) foo (k2) bar (k3)
Example (Uniq) ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var errs []error errs = append(errs, multierror.Tag("k1", fmt.Errorf("foo"))) errs = append(errs, multierror.Tag("k2", fmt.Errorf("foo"))) errs = append(errs, multierror.Tag("k3", fmt.Errorf("bar"))) fmt.Printf("%q", multierror.Uniq(errs)) }
Output: ["foo (k1, k2)" "bar (k3)"]
func TaggedError ¶ added in v0.2.0
TaggedError is like Error() but if err implements TaggedError, it will invoke TaggeddError() and return error message and the tag. Otherwise the tag will be empty.
func Transform ¶ added in v0.2.0
Transform applies a transformer to an unfolded multierror and re-wraps the result.
func Uniq ¶ added in v0.2.0
Uniq deduplicates a list of errors
Example ¶
package main import ( "fmt" "github.com/mkmik/multierror" ) func main() { var errs []error errs = append(errs, fmt.Errorf("foo")) errs = append(errs, fmt.Errorf("bar")) errs = append(errs, fmt.Errorf("foo")) fmt.Printf("%q", multierror.Uniq(errs)) }
Output: ["foo repeated 2 times" "bar"]
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error bundles multiple errors and make them obey the error interface
type Formatter ¶ added in v0.2.0
Formatter allows to customize the rendering of the multierror.
Example ¶
package main import ( "fmt" "strings" "github.com/mkmik/multierror" ) func main() { var err error err = multierror.Append(err, fmt.Errorf("foo")) err = multierror.Append(err, fmt.Errorf("bar")) err = multierror.Append(err, fmt.Errorf("foo")) err = multierror.Transform(err, multierror.Uniq) err = multierror.Format(err, func(errs []string) string { return strings.Join(errs, "; ") }) fmt.Printf("%v", err) }
Output: foo repeated 2 times; bar
type JoinOption ¶ added in v0.3.0
type JoinOption func(*joinOptions)
func WithFormatter ¶ added in v0.3.0
func WithFormatter(f Formatter) JoinOption
func WithTransformer ¶ added in v0.3.0
func WithTransformer(t func([]error) []error) JoinOption