multierror

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 4 Imported by: 20

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

Examples

Constants

This section is empty.

Variables

View Source
var DefaultFormatter = func(errs []string) string {
	buf := bytes.NewBuffer(nil)

	fmt.Fprintf(buf, "%d errors occurred:", len(errs))
	for _, line := range errs {
		fmt.Fprintf(buf, "\n%s", line)
	}

	return buf.String()
}

Functions

func Append deprecated

func Append(err error, errs ...error) error

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

func Fold(errs []error) error

deprecated: Fold is deprecated, use Join instead.

Fold turns a slice of errors into a multierror.

func Format added in v0.2.0

func Format(err error, f Formatter) error

Format sets a custom formatter if err is a multierror.

func InlineFormatter added in v0.2.0

func InlineFormatter(errs []string) string

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

func Split(err error) []error

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

func Tag(tag string, err error) error

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

func TaggedError(err error) (string, string)

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

func Transform(err error, fn func([]error) []error) error

Transform applies a transformer to an unfolded multierror and re-wraps the result.

func Unfold deprecated added in v0.2.0

func Unfold(err error) []error

Deprecated: Unfold is deprecated, use Split instead.

Unfold returns the underlying list of errors wrapped in a multierror. If err is not a multierror, then a singleton list is returned.

func Uniq added in v0.2.0

func Uniq(errs []error) []error

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap added in v0.4.0

func (e *Error) Unwrap() []error

type Formatter added in v0.2.0

type Formatter func(errs []string) string

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

type TaggableError added in v0.2.0

type TaggableError interface {
	// TaggedError is like Error() but splits the error from the tag.
	TaggedError() (string, string)
}

Jump to

Keyboard shortcuts

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