Help us shape the future of Go! Take the 2026 Go Developer Survey

multierror

package module
v0.0.0-...-ea68e72 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2017 License: MPL-2.0 Imports: 3 Imported by: 7

README

go-multierror

go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error.

This allows a function in Go to return an error that might actually be a list of errors. If the caller knows this, they can unwrap the list and access the errors. If the caller doesn't know, the error formats to a nice human-readable format.

This is a fork of the hashicorp go-multierror library. In this fork, nil error values are handled transparently.

Installation and Docs

Install using go get github.com/mspiegel/go-multierror.

Usage

go-multierror is easy to use and purposely built to be unobtrusive in existing Go applications/libraries that may not be aware of it.

Building a list of errors

The Append function is used to create a list of errors. This function behaves a lot like the Go built-in append function: it doesn't matter if the first argument is nil, a multierror.Error, or any other error, the function behaves as you would expect.

var err, result error

err = step1()
result = multierror.Append(result, err)
err = step2()
result = multierror.Append(result, err)

return result

Customizing the formatting of the errors

By specifying a custom ErrorFormat, you can customize the format of the Error() string function:

var result *multierror.Error

// ... accumulate errors here, maybe using Append

if result != nil {
	result.ErrorFormat = func([]error) string {
		return "errors!"
	}
}

Accessing the list of errors

multierror.Error implements error so if the caller doesn't know about multierror, it will work just fine. But if you're aware a multierror might be returned, you can use type switches to access the list of errors:

if err := something(); err != nil {
	if merr, ok := err.(*multierror.Error); ok {
		// Use merr.Errors
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

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

Append is a helper function that will append more errors onto an Error in order to create a larger multi-error.

If err is not a multierror.Error, then it will be turned into one. If any of the errs are multierr.Error, they will be flattened one level into err.

nil values in errs are filtered out. If the err is nil and the length of filtered errs is zero then the function returns nil.

func Flatten

func Flatten(err error) error

Flatten flattens the given error, merging any *Errors together into a single *Error.

func ListFormatFunc

func ListFormatFunc(es []error) string

ListFormatFunc is a basic formatter that outputs the number of errors that occurred along with a bullet point list of the errors.

Types

type Error

type Error struct {
	Errors      []error
	ErrorFormat ErrorFormatFunc
}

Error is an error type to track multiple errors. This is used to accumulate errors in cases and return them as a single "error".

func (*Error) Error

func (e *Error) Error() string

func (*Error) GoString

func (e *Error) GoString() string

func (*Error) WrappedErrors

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

WrappedErrors returns the list of errors that this Error is wrapping. It is an implementatin of the errwrap.Wrapper interface so that multierror.Error can be used with that library.

This method is not safe to be called concurrently and is no different than accessing the Errors field directly. It is implementd only to satisfy the errwrap.Wrapper interface.

type ErrorFormatFunc

type ErrorFormatFunc func([]error) string

ErrorFormatFunc is a function callback that is called by Error to turn the list of errors into a string.

Jump to

Keyboard shortcuts

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