hashicorp / go-multierror

A Go (golang) package for representing a list of errors as a single error.
Mozilla Public License 2.0
2.3k stars 123 forks source link

Returning nil values from Append #42

Closed buth closed 3 years ago

buth commented 3 years ago

At the moment, Append never returns nil. The use case I've run across is the following, where a function returns an array of errors, and it's necessary to return an error if any of them are non-nil.

result, errs := multierrorFunc()
if err := multierror.Append(nil, errs...); err != nil {
    return err
}

I think all that would be needed is to add a check for the length of the error list and update this test.

if len(err.Errors) == 0 {
    return nil
}

If this seems like non-breaking functionality that would be useful, I could submit a pull request.

Thanks all!

mitchellh commented 3 years ago

This is what the ErrorOrNil func is for: https://pkg.go.dev/github.com/hashicorp/go-multierror#Error.ErrorOrNil