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

Return the stdlib error interface #51

Closed astrolox closed 3 years ago

astrolox commented 3 years ago

Thanks for an excellent library!

I'm opening this pull request to propose a several very small changes which are intended to make the Append function "behave as you would expect" (as is suggested in the README).

Changes:

astrolox commented 3 years ago

Fixes #42

mitchellh commented 3 years ago

Thank you for the PR. Up front, this PR unfortunately can't be merged without an associated major version bump (v2). Although the it is a "small change" it is still a breaking change and since we use Go modules and use this library heavily expecting the *Error type to be returned, this would require a major version bump.

Your reasoning on idiomatic Go is completely valid, however we did make this return value the way we did purposefully and thoughtfully based on our call style. We wanted to avoid (or exactly control) the nil is not nil situation and we commonly initialize an empty *Error to setup formatting or some other thing and call append on it throughout (potentially with nil errors).

The function you're looking for is probably ErrorOrNil which is what we always call on return (return merr.ErrorOrNil()) which returns the stdlib error type as you expect. This is safe to call on a nil *Error.

I think if we were building this library from scratch, we may consider changing the semantics as you've proposed. I don't disagree with them. However, in the context of a library that has existed for many years and is already deeply in use in dozens of our own products (and thousands of other downstreams according to pkg.go.dev), I don't think the change justifies the breaking change and major version bump.

astrolox commented 3 years ago

No problem. Thank you for the quick reply. I'll maintain a fork for my own use.