Skip to content

Commit

Permalink
Merge pull request #16 from zostay/deferred-error
Browse files Browse the repository at this point in the history
feat: adding deferred.Error()
  • Loading branch information
zostay authored Jun 14, 2024
2 parents bf41a03 + f8de641 commit 28843c9
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prepare.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.20
- name: Release Verseion
run: echo $RELEASE_VERSION=$(echo $GITHUB_REF_NAME | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+.*$') >> $GITHUB_ENV
- name: Version Check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.20
- name: Release Verseion
run: echo $RELEASE_VERSION=$(echo $GITHUB_REF_NAME | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+.*$') >> $GITHUB_ENV
- name: Version Check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: '1.20'
cache: false
- uses: golangci/golangci-lint-action@v6
with:
Expand Down
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## WIP TBD

* :boom: Breaking Change :boom:: Now requires Go 1.20.
* Adding deferred.Error for helping with deferred error handling.

## v0.6.0 2023-08-12

* Adding slices.Insert
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ There are a lot of common operations simply missing from the Golang builtin
library. This makes up for that deficiency. Here's a summary of some of the
provided tools:

## Deferred Handling

Handling deferred functions is a bit of a pain. This provides a way to handle
certain deferred calls with a little less hassle:

* `Error` (allows capturing both errors that may occur during defer)

## FileSystem Operations

The built-in `io/fs` package is fine for reading files, but is missing write interfaces. This provides them:
Expand Down
33 changes: 33 additions & 0 deletions deferred/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package deferred

import "errors"

// Error is intended to be used in cases where you have a deferred function that
// returns an error and you want to capture that error, but you may have an error
// that has already occurred and need to capture that too:
//
// func ProcessFile(name string) (err error) {
// var r io.Reader
// r, err = os.Open(name)
// if err != nil {
// return
// }
// defer deferred.Error(&err, r.Close())
//
// // process file
// return
// }
//
// Now, both errors can be captured without a lot of gymnastics.
func Error(err *error, deferErr error) {
if deferErr == nil {
return
}

if *err == nil {
*err = deferErr
return
}

*err = errors.Join(*err, deferErr)
}
52 changes: 52 additions & 0 deletions deferred/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package deferred_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/zostay/go-std/deferred"
)

func TestError(t *testing.T) {
t.Parallel()

errors := []error{
fmt.Errorf("error 1"),
fmt.Errorf("error 2"),
}

cases := []struct {
first, second error
name string
}{
{nil, nil, "nil-nil"},
{errors[0], nil, "error-nil"},
{nil, errors[0], "nil-error"},
{errors[0], errors[1], "error-error"},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

err := c.first
deferred.Error(&err, c.second)

if c.first == nil && c.second == nil {
assert.NoError(t, err, c.name)
return
}

assert.Error(t, err)
if c.first != nil {
assert.ErrorIs(t, err, c.first)
}
if c.second != nil {
assert.ErrorIs(t, err, c.second)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zostay/go-std

go 1.19
go 1.20

require (
github.com/stretchr/testify v1.8.2
Expand Down

0 comments on commit 28843c9

Please sign in to comment.