multierror

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 10 Imported by: 42

Documentation

Overview

Package multierror can be leveraged as an opinionated to handle multiple errors providing appropriate wrapping for them.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONFormatFunc added in v1.1.0

func JSONFormatFunc(es []error) string

JSONFormatFunc takes in a list of errors and encodes each error to JSON. If the error is not encodable to JSON, then the error is transformed to the json format: `{"message": "err.Error()"}`.

func WithFormat added in v1.1.0

func WithFormat(err error, format string) error

WithFormat is convenient a helper to modify the multierror format function to the JSON format, if the format isn't "json", then the unmodified error is returned.

func WithFormatFunc added in v1.1.0

func WithFormatFunc(err error, ff FormatFunc) error

WithFormatFunc takes in an error and tries to set the ErrorFormatFunc to the passed function if the error is of type *Prefixed or *multierror.Error, otherwise it returns the error as is.

Types

type FormatFunc

type FormatFunc func(es []error) string

FormatFunc defines a format function which should format a slice of errors into a string.

type JSONError added in v1.1.0

type JSONError struct {
	Errors []interface{} `json:"errors,omitempty"`
}

JSONError wraps a list of errors to be encoded in JSON format.

type Prefixed

type Prefixed struct {
	Prefix     string
	Errors     []error
	FormatFunc FormatFunc

	// SkipPrefixing when set to true subsequently appended errors needn't be
	// prefixed. This is particularly useful multierrors which contain JSON
	// marshaleable errors.
	SkipPrefixing bool
	// contains filtered or unexported fields
}

Prefixed is a multierror which will prefix the error output message with the specified prefix. The output will be sorted so the errors can be asserted on test functions in case the multierror has been used in different goroutines. This structure is concurrently safe, avoid using value semantics.

Example
err := NewPrefixed("config validation")
err = err.Append(errors.New("some validation error"))

if err.ErrorOrNil() != nil {
	fmt.Fprintln(output, err)
}
Output:

Example (Json)
err := NewPrefixed("config validation")
err = err.Append(errors.New("some validation error"))

if err.ErrorOrNil() != nil {
	fmt.Fprintln(output, WithFormat(err, "json"))
}
Output:

func NewJSONPrefixed added in v1.1.0

func NewJSONPrefixed(prefix string, errs ...error) *Prefixed

NewJSONPrefixed returns a new pointer to Prefixed with the right config for JSON packed errors to not be prefixed.

func NewPrefixed

func NewPrefixed(prefix string, errs ...error) *Prefixed

NewPrefixed creates a new pointer to Prefixed.

func (*Prefixed) Append

func (p *Prefixed) Append(errs ...error) *Prefixed

Append appends a number of errors to the current instance of Prefixed. It'll unwrap any wrapped errors in the form of *Prefixed or *multierror.Error.

func (*Prefixed) Error

func (p *Prefixed) Error() string

Error returns the stored slice of error formatted using a set FormatFunc or multierror.ListFormatFunc when no FormatFunc is specified.

func (*Prefixed) ErrorOrNil

func (p *Prefixed) ErrorOrNil() error

ErrorOrNil either returns nil when the type is nil or when there's no Errors. Otherwise, the type is returned.

Jump to

Keyboard shortcuts

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