-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from zostay/deferred-error
feat: adding deferred.Error()
- Loading branch information
Showing
8 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|