From c3735127ae69bac2201a51810f20507e0136b14f Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 19 Nov 2024 16:50:12 -0500 Subject: [PATCH] allow FlattenMultierror to accept standard error --- helper/funcs.go | 7 ++++++- helper/funcs_test.go | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/helper/funcs.go b/helper/funcs.go index 4dbf1223c..5b394c09f 100644 --- a/helper/funcs.go +++ b/helper/funcs.go @@ -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 // 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 { return nil } diff --git a/helper/funcs_test.go b/helper/funcs_test.go index 9b4e54265..86e6fd4c8 100644 --- a/helper/funcs_test.go +++ b/helper/funcs_test.go @@ -488,11 +488,21 @@ func Test_SliceSetEq(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 + err = FlattenMultierror(mErr0) + must.Nil(t, err) + mErr0 = multierror.Append(mErr0, func() error { return nil }()) - err := FlattenMultierror(mErr0) + err = FlattenMultierror(mErr0) must.Nil(t, err) var mErr1 *multierror.Error