Skip to content

Commit

Permalink
Preserve output value when only when error is produced
Browse files Browse the repository at this point in the history
  • Loading branch information
mspiegel authored and Michael Spiegel committed Oct 19, 2016
1 parent 4ad7911 commit a907df9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion append.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ func AppendNonNil(err error, errs ...error) error {

errs = removeNils(errs)
// Preserve input value when no errors have occurred
// Preserve output value when only one error is produced
if len(errs) == 0 {
return err
} else if (err == nil) && len(errs) == 1 {
return errs[0]
}

return Append(err, errs...)
}

Expand Down
5 changes: 5 additions & 0 deletions append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func TestAppendNonNil(t *testing.T) {
if result != err1 {
t.Fatalf("input error modified: %s", result.Error())
}
err1 = errors.New("foo")
result = AppendNonNil(nil, err1, nil, nil)
if result != err1 {
t.Fatalf("input error modified: %s", result.Error())
}
}

func TestAppendNonNilStruct(t *testing.T) {
Expand Down

0 comments on commit a907df9

Please sign in to comment.