hashicorp / go-multierror

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

Preserve nil value when no errors have occurred. #7

Closed mspiegel closed 8 years ago

mspiegel commented 8 years ago

This patch handles nil values when passed into the slice argument of Append(err error, errs ...error). nil values are filtered out and not included in the multierror. When the first argument to Append is nil, and the second argument consists entirely of nil values, then return a nil value.

This patch allows the usage the following style.

var result error

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

return result

result will be nil if-and-only-if step1() and step2() both return nil.

I believe this change is backwards compatible. The old behavior when nil values were passed into the slice argument of Append() was to wrap the nil values inside error objects. If this change is not backwards compatible I can revise the patch to instead declare a new function AppendNonNill(err error, errs ...error)