diff --git a/append.go b/append.go index 0112164..ca179a4 100644 --- a/append.go +++ b/append.go @@ -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...) } diff --git a/append_test.go b/append_test.go index 61dd5dc..79ffecc 100644 --- a/append_test.go +++ b/append_test.go @@ -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) {