Documentation ¶
Overview ¶
MultiError lets you wrap multiple errors into one error. Use this for iterators that don't necessarily bail when they hit an error, but want to return all of the encountered errors to the caller.
MultiError is a typed []error that implements golang's error interface. As such, you can use slice operations to modify or inspect the list of contained errors. Error() attempts to provide coherent stringification based on the number of errors in the MultiError.
Example ¶
err := New() err = append(err, errors.New("Add errors to MultiError by append-ing")) fmt.Println(err) err = make(MultiError, 0) err = append(err, errors.New("It's fine to make MultiErrors with make")) err = append(err, errors.New("Error() will let you know how many errors are contained.")) fmt.Printf("%s", err)
Output: Add errors to MultiError by append-ing It's fine to make MultiErrors with make (and 1 other error)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
// The string output by Error() when the list has no errors.
NoErrorsMessage = "(no errors)"
)
Functions ¶
This section is empty.
Types ¶
type MultiError ¶
type MultiError []error
func New ¶
func New(errors ...error) MultiError
Use new to instantiate a new MultiError or just
make(MultiError, 0)
func (MultiError) Error ¶
func (m MultiError) Error() string
The error message returned by Error() always contains the error message of the first error in the list (if there is one). If len(MultiError) > 1 the number of errors in the list is also noted. If zero, the NoErrorsMessage variable is used.
func (MultiError) NilWhenEmpty ¶
func (m MultiError) NilWhenEmpty() error
Returns the original MultiError if there's at least one error inside it, or nil if there isn't. Saves you from having to check len(MultiError) when returning from your function.
func Worker() error { merr := make(MultiError, 0) for _, item := range work { err := doSomething() if err != nil { merr = append(merr, err) } } return merr.NilWhenEmpty() }