formatting: %+v should apply to nested errors (#5)
`%+v` on a multierr should use `%+v` on the nested errors. That is,
fmt.Sprintf("%+v", multierr.Combine(err1, err2))
Should use `fmt.Sprintf("%+v", err1)` and `fmt.Sprinf("%+v", err2)` in
the output rather than `err{1, 2}.Error()`.
diff --git a/error.go b/error.go
index cf4612f..1e1fe28 100644
--- a/error.go
+++ b/error.go
@@ -109,7 +109,7 @@
w.Write(_multilinePrefix)
for _, item := range merr {
w.Write(_multilineSeparator)
- writePrefixLine(w, _multilineIndent, item.Error())
+ writePrefixLine(w, _multilineIndent, fmt.Sprintf("%+v", item))
}
}
diff --git a/error_test.go b/error_test.go
index 4c72c45..67b04bc 100644
--- a/error_test.go
+++ b/error_test.go
@@ -3,12 +3,29 @@
import (
"errors"
"fmt"
+ "io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
+// richFormatError is an error that prints a different output depending on
+// whether %v or %+v was used.
+type richFormatError struct{}
+
+func (r richFormatError) Error() string {
+ return fmt.Sprint(r)
+}
+
+func (richFormatError) Format(f fmt.State, c rune) {
+ if c == 'v' && f.Flag('+') {
+ io.WriteString(f, "multiline\nmessage\nwith plus")
+ } else {
+ io.WriteString(f, "without plus")
+ }
+}
+
func TestCombine(t *testing.T) {
tests := []struct {
giveErrors []error
@@ -155,6 +172,25 @@
" - bar",
wantSingleline: "foo; bar",
},
+ {
+ giveErrors: []error{
+ errors.New("foo"),
+ richFormatError{},
+ errors.New("bar"),
+ },
+ wantError: multiError{
+ errors.New("foo"),
+ richFormatError{},
+ errors.New("bar"),
+ },
+ wantMultiline: "the following errors occurred:\n" +
+ " - foo\n" +
+ " - multiline\n" +
+ " message\n" +
+ " with plus\n" +
+ " - bar",
+ wantSingleline: "foo; without plus; bar",
+ },
}
for i, tt := range tests {