-
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 #11 from Belphemur/renovate
perf(error): better deal with deferred errors
- Loading branch information
Showing
6 changed files
with
131 additions
and
53 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
File renamed without changes.
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,16 @@ | ||
package errs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Capture runs errFunc and assigns the error, if any, to *errPtr. Preserves the | ||
// original error by wrapping with errors.Join if the errFunc err is non-nil. | ||
func Capture(errPtr *error, errFunc func() error, msg string) { | ||
err := errFunc() | ||
if err == nil { | ||
return | ||
} | ||
*errPtr = errors.Join(*errPtr, fmt.Errorf("%s: %w", msg, err)) | ||
} |
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,58 @@ | ||
package errs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCapture(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
initial error | ||
errFunc func() error | ||
msg string | ||
expected string | ||
}{ | ||
{ | ||
name: "No error from errFunc", | ||
initial: nil, | ||
errFunc: func() error { return nil }, | ||
msg: "test message", | ||
expected: "", | ||
}, | ||
{ | ||
name: "Error from errFunc with no initial error", | ||
initial: nil, | ||
errFunc: func() error { return errors.New("error from func") }, | ||
msg: "test message", | ||
expected: "test message: error from func", | ||
}, | ||
{ | ||
name: "Error from errFunc with initial error", | ||
initial: errors.New("initial error"), | ||
errFunc: func() error { return errors.New("error from func") }, | ||
msg: "test message", | ||
expected: "initial error\ntest message: error from func", | ||
}, | ||
{ | ||
name: "Error from errFunc with initial wrapped error", | ||
initial: fmt.Errorf("wrapped error: %w", errors.New("initial error")), | ||
errFunc: func() error { return errors.New("error from func") }, | ||
msg: "test message", | ||
expected: "wrapped error: initial error\ntest message: error from func", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var err error = tt.initial | ||
Capture(&err, tt.errFunc, tt.msg) | ||
if err != nil && err.Error() != tt.expected { | ||
t.Errorf("expected %q, got %q", tt.expected, err.Error()) | ||
} else if err == nil && tt.expected != "" { | ||
t.Errorf("expected %q, got nil", tt.expected) | ||
} | ||
}) | ||
} | ||
} |