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 <albinker@gmail.com>
This commit is contained in:
Albin Kerouanton 2023-08-19 13:01:01 +02:00
parent 01ac4892e0
commit 173e9ba38f
No known key found for this signature in database
GPG key ID: 630B8E1DCBDB1864
2 changed files with 4 additions and 5 deletions

View file

@ -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))

View file

@ -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