Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit e953dfe

Browse files
mwiczersongy23
authored andcommitted
Fix bug in internal.CombineErrors (#553)
Previously, multiple errors would produce a bunch of leading "; "s. e.g. CombineErrors([foo bar]) = "[; ; foo; bar]"
1 parent 5f5a84c commit e953dfe

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

internal/internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func CombineErrors(errs []error) error {
4040
if numErrors == 1 {
4141
return errs[0]
4242
} else if numErrors > 1 {
43-
errMsgs := make([]string, numErrors)
43+
errMsgs := make([]string, 0, numErrors)
4444
for _, err := range errs {
4545
errMsgs = append(errMsgs, err.Error())
4646
}

internal/internal_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package internal_test
1616

1717
import (
18+
"fmt"
1819
"testing"
1920
"time"
2021

@@ -38,3 +39,35 @@ func TestTimeConverters(t *testing.T) {
3839
t.Errorf("Convertedback time does not match original time\nGot: %d\nWant:%d", g, w)
3940
}
4041
}
42+
43+
func TestCombineErrors(t *testing.T) {
44+
testCases := []struct {
45+
errors []error
46+
expected string
47+
expectNil bool
48+
}{{
49+
errors: []error{},
50+
expectNil: true,
51+
}, {
52+
errors: []error{
53+
fmt.Errorf("foo"),
54+
},
55+
expected: "foo",
56+
}, {
57+
errors: []error{
58+
fmt.Errorf("foo"),
59+
fmt.Errorf("bar"),
60+
},
61+
expected: "[foo; bar]",
62+
}}
63+
64+
for _, tc := range testCases {
65+
got := internal.CombineErrors(tc.errors)
66+
if (got == nil) != tc.expectNil {
67+
t.Errorf("CombineErrors(%v) == nil? Got: %t. Want: %t", tc.errors, got == nil, tc.expectNil)
68+
}
69+
if got != nil && tc.expected != got.Error() {
70+
t.Errorf("CombineErrors(%v) = %q. Want: %q", tc.errors, got, tc.expected)
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)