From e1e913ffc6e2bac5ddcc7546e7b5e09c91bca8ad Mon Sep 17 00:00:00 2001 From: Alexandr Burdiyan Date: Thu, 1 Sep 2022 10:18:11 +0200 Subject: [PATCH 1/3] Add a shorthand for AppendInvoke --- error.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/error.go b/error.go index 5c2c9e1..c2fb271 100644 --- a/error.go +++ b/error.go @@ -660,3 +660,10 @@ func Close(closer io.Closer) Invoker { func AppendInvoke(into *error, invoker Invoker) { AppendInto(into, invoker.Invoke()) } + +// AppendFunc is a shorthand for AppendInvoke. +// It allows using function or method value directly +// without having to wrap it into an Invoker interface. +func AppendFunc(into *error, fn func() error) { + AppendInvoke(into, Invoke(fn)) +} From e06fb977c5ed4ad9d24e7eaa984d3f4d7418aab9 Mon Sep 17 00:00:00 2001 From: Alexandr Burdiyan Date: Sun, 4 Sep 2022 21:38:10 +0200 Subject: [PATCH 2/3] Apply code review suggestions Co-authored-by: Sung Yoon Whang Apply code review suggestions Co-authored-by: Sung Yoon Whang Adding tests --- error.go | 16 ++++++++++++++-- error_test.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/error.go b/error.go index c2fb271..cdd91ae 100644 --- a/error.go +++ b/error.go @@ -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)) } diff --git a/error_test.go b/error_test.go index c053167..ddb1208 100644 --- a/error_test.go +++ b/error_test.go @@ -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 } From d260cb708a588a1dfa2b9db4bb514e4fd4a41bb0 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Sun, 4 Sep 2022 21:16:37 -0700 Subject: [PATCH 3/3] Update error_test.go --- error_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/error_test.go b/error_test.go index ddb1208..39ca313 100644 --- a/error_test.go +++ b/error_test.go @@ -685,13 +685,11 @@ func TestAppendFunc(t *testing.T) { return errDeferred } - do := func() (err error) { + err := 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") }