allow FlattenMultierror to accept standard error

This commit is contained in:
Tim Gross
2024-11-19 16:50:12 -05:00
parent 7c85176059
commit c3735127ae
2 changed files with 17 additions and 2 deletions

View File

@@ -528,7 +528,12 @@ func Merge[T comparable](a, b T) T {
// FlattenMultierror takes a multierror and unwraps it if there's only one error // FlattenMultierror takes a multierror and unwraps it if there's only one error
// in the output, otherwise returning the multierror or nil. // in the output, otherwise returning the multierror or nil.
func FlattenMultierror(mErr *multierror.Error) error { func FlattenMultierror(err error) error {
mErr, ok := err.(*multierror.Error)
if !ok {
return err
}
// note: mErr is a pointer so we still need to nil-check even after the cast
if mErr == nil { if mErr == nil {
return nil return nil
} }

View File

@@ -488,11 +488,21 @@ func Test_SliceSetEq(t *testing.T) {
func TestFlattenMultiError(t *testing.T) { func TestFlattenMultiError(t *testing.T) {
err := FlattenMultierror(nil)
must.Nil(t, err)
err0 := errors.New("oh no!")
err = FlattenMultierror(err0)
must.Eq(t, `oh no!`, err.Error())
var mErr0 *multierror.Error var mErr0 *multierror.Error
err = FlattenMultierror(mErr0)
must.Nil(t, err)
mErr0 = multierror.Append(mErr0, func() error { mErr0 = multierror.Append(mErr0, func() error {
return nil return nil
}()) }())
err := FlattenMultierror(mErr0) err = FlattenMultierror(mErr0)
must.Nil(t, err) must.Nil(t, err)
var mErr1 *multierror.Error var mErr1 *multierror.Error