multierror

package module
v0.0.0-...-0821ff0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2018 License: Apache-2.0 Imports: 1 Imported by: 2

README

multierror

Go package implementing an error that can contain multiple errors

Go Documentation

multierror wraps multiple errors into a typed slice that implements Go's error interface. It's useful for iterators that don't bail when they encounter an error, but still need to return error information to the caller.

multierror.MultiError aims toward leanness and is mainly utilized with a combination of error and slice semantics.

Installation

go get github.com/efixler/multierror

Usage

import (
	"github.com/efixler/multierror"
)

func Worker() error {
   merr := multierror.New()
   for _, item := range work {
	 err := doSomething()
	 if err != nil {
	   merr = append(merr, err)
	 }
    }
    return merr.NilWhenEmpty()
 }

See the Godoc for details and more examples.

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

View Source
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()
 }

Jump to

Keyboard shortcuts

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