Skip to content

Commit

Permalink
Merge pull request #10 from upfluence/am/multi-wrap
Browse files Browse the repository at this point in the history
multi: Improve multi error recursive wrapping
  • Loading branch information
AlexisMontagne authored Sep 20, 2023
2 parents 2daa935 + 3676239 commit 445b40d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 10 additions & 9 deletions multi/multi_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (

type multiError []error

func (errs multiError) Errors() []error {
return errs
}
func (errs multiError) Unwrap() []error { return errs }
func (errs multiError) Errors() []error { return errs }

func (errs multiError) Error() string {
var b strings.Builder
Expand Down Expand Up @@ -66,11 +65,7 @@ func Wrap(errs []error) error {
continue
}

if e, ok := err.(MultiError); ok {
merrs = append(merrs, e.Errors()...)
} else {
merrs = append(merrs, err)
}
merrs = append(merrs, ExtractErrors(err)...)
}

switch len(merrs) {
Expand Down Expand Up @@ -106,5 +101,11 @@ func ExtractErrors(err error) []error {
return []error{err}
}

return ExtractErrors(nerr)
errs := ExtractErrors(nerr)

if len(errs) == 1 && errs[0] == nerr {
return []error{err}
}

return errs
}
7 changes: 7 additions & 0 deletions multi/multi_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/upfluence/errors"
"github.com/upfluence/errors/multi"
"github.com/upfluence/errors/tags"
)

Expand All @@ -27,6 +28,12 @@ func TestCombining(t *testing.T) {
{errs: []error{}},
{errs: []error{nil, nil, errors.Combine(nil, foo)}, want: foo},
{errs: []error{foo}, want: foo},
{
errs: []error{
foo, errors.Combine(foo, foo),
},
want: multi.Combine(foo, foo, foo),
},
} {
assert.Equal(t, tt.want, errors.Cause(errors.WrapErrors(tt.errs)))
}
Expand Down

0 comments on commit 445b40d

Please sign in to comment.