From 173e9ba38f23b77e8205e67bdc89010d4923f814 Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Sat, 19 Aug 2023 13:01:01 +0200 Subject: [PATCH] multierror: make it consistent when there's a single error If a single error is passed to `multierror.Join`, `joinError.Error()` returns the error as is. However, due to the multiline formatting applied otherwise, it's better to add a line break between the preamble and the multierror string when wrapping it, eg.: ```golang fmt.Errorf("invalid network config:\n%w", multierror.Join(errs...)) ``` This commit removes this special case to make the format consistent no matter how many errors are joined: ``` invalid network config: some error invalid network config: * some error ``` Signed-off-by: Albin Kerouanton --- internal/multierror/multierror.go | 3 --- internal/multierror/multierror_test.go | 6 ++++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/multierror/multierror.go b/internal/multierror/multierror.go index cf4d6a5957..b8ac2bd6a7 100644 --- a/internal/multierror/multierror.go +++ b/internal/multierror/multierror.go @@ -31,9 +31,6 @@ type joinError struct { } func (e *joinError) Error() string { - if len(e.errs) == 1 { - return strings.TrimSpace(e.errs[0].Error()) - } stringErrs := make([]string, 0, len(e.errs)) for _, subErr := range e.errs { stringErrs = append(stringErrs, strings.Replace(subErr.Error(), "\n", "\n\t", -1)) diff --git a/internal/multierror/multierror_test.go b/internal/multierror/multierror_test.go index 2d46240197..efe4d2e327 100644 --- a/internal/multierror/multierror_test.go +++ b/internal/multierror/multierror_test.go @@ -10,10 +10,12 @@ import ( func TestErrorJoin(t *testing.T) { t.Run("single", func(t *testing.T) { - err := Join(fmt.Errorf("invalid config: %w", Join(errors.New("foo")))) - const expected = `invalid config: foo` + err := fmt.Errorf("invalid config:\n%w", Join(errors.New("foo"))) + const expected = `invalid config: +* foo` assert.Equal(t, err.Error(), expected) }) + t.Run("multiple", func(t *testing.T) { err := Join(errors.New("foobar"), fmt.Errorf("invalid config: \n%w", Join(errors.New("foo"), errors.New("bar")))) const expected = `* foobar