multierror: github.com/gogolfing/multierror Index | Examples | Files

package multierror

import "github.com/gogolfing/multierror"

Package multierror provides a single type that is simply a slice of errors.

This would be beneficial if multiple errors occur in a single function, and all would need to be returned in some unit as a single error type.

Index

Examples

Package Files

multierror.go

type MultiError Uses

type MultiError []error

Type MultiError is simply a slice of errors. Each error in the slice is included in the output of Error().

IMPORTANT: a value of MultiError([]error{}) is obviously a slice of length 0 and not nil. Therefore an err != nil comparison may be semantically confusing since the value is not nil, but there are no errors contained within. Additionally, be aware of implicit casts from function returns of this type. See New() and Append() for examples of how MultiError types can be non-nil.

func Append Uses

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

Append returns a MultiError given an initial error and variadic slice of errors. This is a separate implementation of New().Append(...) if done following the example. This can ensure that an empty MultiError is in fact nil.

Code:

outer := func() error {
    var result error
    f := func() error {
        return nil
    }
    if err := f(); err != nil {
        result = multierror.Append(result, err)
    }
    return result
}
err := outer()
if err != nil {
    fmt.Println(err.Error())
} else {
    fmt.Println("error is nil")
}

Output:

error is nil

func New Uses

func New(errs ...error) MultiError

New provides an easy way to create a new MultiError. If errs is empty, then nil is returned. This is implemented by creating a nil MultiError and calling Append(errs...). nil errs are excluded.

Unfortunately, because MultiError is an error type, casting it as an error type can cause unexpected results. Thus New should only be called with non-nil errors.

f := func() error {
	return multierror.New()
}
err := f()
//err will be considered non-nil at this point.

Code:

err := multierror.New(
    fmt.Errorf("this is an error"),
    fmt.Errorf("this is another error"),
)
fmt.Println(err.Error())

Output:

this is an error
this is another error

Code:

f := func() error {
    return multierror.New()
}
err := f()
if err != nil {
    fmt.Println("this is an error for the empty call")
    fmt.Println(err.Error())
} else {
    fmt.Println("there is no error for the empty call")
}

f = func() error {
    return multierror.New(nil)
}
err = f()
if err != nil {
    fmt.Println("this is an error for the nil call")
    fmt.Println(err.Error())
} else {
    fmt.Println("there is no error for the nil call")
}

fmt.Println("done")

Output:

this is an error for the empty call

this is an error for the nil call

done

func (MultiError) Append Uses

func (m MultiError) Append(errs ...error) MultiError

Append appends all errors in errs (that are not nil) to m. This is semantically equivalent to the builtin append() function. If errs is empty or contains only nil errors, then m is returned. Otherwise, all errors (that are not nil) are appended and returned. Note that all nil errors are excluded from the result.

Code:

err := multierror.New(fmt.Errorf("first error"))
err = err.Append(nil)
err = err.Append(fmt.Errorf("second error"))
fmt.Println(err.Error())

Output:

first error
second error

func (MultiError) Error Uses

func (m MultiError) Error() string

Error is the error interface function. The result is what is returned from m.Join("\n"). The default behaviour is to return all results of each error.Error() on its own line.

func (MultiError) ErrorOrNil Uses

func (m MultiError) ErrorOrNil() error

ErrorOrNil returns an error (of type MultiError) if there are errors, or nil if the slice is empty.

Code:

f := func() error {
    result := multierror.New()
    return result.ErrorOrNil()
}
err := f()
if err != nil {
    fmt.Println(err.Error())
} else {
    fmt.Println("error is nil")
}

Output:

error is nil

func (MultiError) Join Uses

func (m MultiError) Join(join string) string

Join joins the individual error's Error() results using strings.Join(m.Raw(), join). join is the string separator put between all error.Error() results.

Code:

f := func() error {
    return multierror.New(
        fmt.Errorf("first error"),
        fmt.Errorf("second error"),
    )
}
err := f()
if err != nil {
    fmt.Println(err.Error())
    if multiErr, ok := err.(multierror.MultiError); ok {
        fmt.Println(multiErr.Join(", "))
    }
}

Output:

first error
second error
first error, second error

func (MultiError) Raw Uses

func (m MultiError) Raw() []string

Raw returns a slice of strings that are the results of each error.Error() result. If m is nil, then nil is returned.

Code:

err := multierror.New(
    fmt.Errorf("first error"),
    fmt.Errorf("second error"),
)
fmt.Printf("%q", err.Raw())

Output:

["first error" "second error"]

Package multierror imports 1 packages (graph). Updated 2016-08-14. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).