Skip to content

Commit

Permalink
Apply code review suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>

Apply code review suggestions

Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>

Adding tests
  • Loading branch information
burdiyan committed Sep 4, 2022
1 parent e1e913f commit e06fb97
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions error.go
Expand Up @@ -661,9 +661,21 @@ func AppendInvoke(into *error, invoker Invoker) {
AppendInto(into, invoker.Invoke())
}

// AppendFunc is a shorthand for AppendInvoke.
// AppendFunc is a shorthand for [AppendInvoke].
// It allows using function or method value directly
// without having to wrap it into an Invoker interface.
// without having to wrap it into an [Invoker] interface.
//
// func doSomething(...) (err error) {
// w, err := startWorker(...)
// if err != nil {
// return err
// }
//
// // multierr will call w.Stop() when this function returns and
// // if the operation fails, it appends its error into the
// // returned error.
// defer multierr.AppendFunc(&err, w.Stop)
// }
func AppendFunc(into *error, fn func() error) {
AppendInvoke(into, Invoke(fn))
}
20 changes: 20 additions & 0 deletions error_test.go
Expand Up @@ -675,6 +675,26 @@ func TestAppendIntoNil(t *testing.T) {
})
}

func TestAppendFunc(t *testing.T) {
var (
errDeferred = errors.New("deferred func called")
errOriginal = errors.New("original error")
)

stopFunc := func() error {
return errDeferred
}

do := func() (err error) {
defer AppendFunc(&err, stopFunc)

return errOriginal
}

err := do()
assert.Equal(t, []error{errOriginal, errDeferred}, Errors(err), "both deferred and original error must be returned")
}

func errorPtr(err error) *error {
return &err
}
Expand Down

0 comments on commit e06fb97

Please sign in to comment.