Skip to content

Commit

Permalink
💥 Merge errs ignore (#36)
Browse files Browse the repository at this point in the history
Add Ignore error function to be used with defer. Ignore executes nullary
input function f and ignores its error return value. A typical use case
is ignoring errors in the defer statements:

	defer errs.Ignore(body.Close)

* errs: Add Ignore function

 errs/ignore.go      |  9 +++++++++
 errs/ignore_test.go | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)
  • Loading branch information
juliaogris committed Sep 28, 2020
2 parents 30a869d + 1ed685a commit fffff66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions errs/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package errs

// Ignore executes nullary input function f and ignores its error return
// value. A typical use case is ignoring errors in the defer statements:
//
// defer errs.Ignore(body.Close)
func Ignore(f func() error) {
_ = f()
}
18 changes: 18 additions & 0 deletions errs/ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errs

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
)

func TestIgnore(t *testing.T) {
executed := false
f := func() error {
executed = true
return errors.New("💥")
}
Ignore(f)
require.True(t, executed)
}

0 comments on commit fffff66

Please sign in to comment.