-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat: add Capturef #27207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gwossum
wants to merge
2
commits into
master-1.x
Choose a base branch
from
gw/errors_capturef
base: master-1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: add Capturef #27207
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,9 @@ | ||
| version: "2" | ||
|
|
||
| linters: | ||
| settings: | ||
| govet: | ||
| settings: | ||
| printf: | ||
| funcs: | ||
| - Capturef |
This file contains hidden or 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,5 +1,10 @@ | ||
| package errors | ||
|
|
||
| import ( | ||
| stderrors "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // Capture is a wrapper function which can be used to capture errors from closing via a defer. | ||
| // An example: | ||
| // | ||
|
|
@@ -19,3 +24,47 @@ func Capture(rErr *error, fn func() error) func() { | |
| } | ||
| } | ||
| } | ||
|
|
||
| // Capturef is similar to Capture but allows adding context when an error occurs. | ||
| // If an fn returns an error, then a new error is creating by concatenating ": %w" | ||
| // to fs and adding the error to the end of a. | ||
| // | ||
| // An additional difference to Capture is that if fn() returns an error, the | ||
| // new error is joined to the existing rErr using errors.Join. | ||
| // | ||
| // func Example(fn string) (rErr error) { | ||
| // f, err := os.Open(fn) | ||
| // if err != nil { | ||
| // return err | ||
| // } | ||
| // defer errors.Capturef(&rErr, f.Close, "error closing %q", fn)() | ||
| // .... | ||
| // } | ||
| // | ||
| // As an illustration, if Example is called with Example("meta.boltdb") and f.Close | ||
| // fails with a disk full error (syscall.ENOSPC), then the error string returned | ||
| // would be `error closing "meta.boltdb": no space left on device` and | ||
| // errors.Is(Example("meta.boltdb"), syscall.ENOSPC) would return true. | ||
| // | ||
| // If fs is empty, a is ignored and the error returned by fn() is appended | ||
| // to rErr unmodified. | ||
| func Capturef(rErr *error, fn func() error, fs string, a ...any) func() { | ||
| return func() { | ||
|
Comment on lines
+51
to
+52
|
||
| if fn != nil { | ||
| if err := fn(); err != nil { | ||
| if rErr != nil { | ||
| // Eventually, the behavior regarding an empty fs will allow | ||
| // Capture to be rewritten as a simple wrapper around Capturef. | ||
| // This can happen once we decide Capture should use errors.Join. | ||
| if fs != "" { | ||
| args := make([]any, 0, len(a)+1) | ||
| args = append(args, a...) | ||
| args = append(args, err) | ||
| err = fmt.Errorf(fs+": %w", args...) | ||
| } | ||
| *rErr = stderrors.Join(*rErr, err) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,202 @@ | ||
| package errors | ||
|
|
||
| import ( | ||
| stderrors "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCapture(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
| errOriginal := stderrors.New("original error") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| origErr error | ||
| closeErr error | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "nil original, nil close", | ||
| origErr: nil, | ||
| closeErr: nil, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "nil original, non-nil close", | ||
| origErr: nil, | ||
| closeErr: errClose, | ||
| wantErr: errClose, | ||
| }, | ||
| { | ||
| name: "non-nil original, nil close", | ||
| origErr: errOriginal, | ||
| closeErr: nil, | ||
| wantErr: errOriginal, | ||
| }, | ||
| { | ||
| name: "non-nil original, non-nil close", | ||
| origErr: errOriginal, | ||
| closeErr: errClose, | ||
| wantErr: errOriginal, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := tt.origErr | ||
| fn := Capture(&err, func() error { | ||
| return tt.closeErr | ||
| }) | ||
| fn() | ||
|
|
||
| if tt.wantErr == nil { | ||
| require.NoError(t, err) | ||
| } else { | ||
| require.ErrorIs(t, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCapture_DeferPattern(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
|
|
||
| fn := func() (err error) { | ||
| defer Capture(&err, func() error { | ||
| return errClose | ||
| })() | ||
| return nil | ||
| } | ||
|
|
||
| require.ErrorIs(t, fn(), errClose) | ||
| } | ||
|
|
||
| func TestCapture_DeferPatternPreservesOriginal(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
| errOriginal := stderrors.New("original error") | ||
|
|
||
| fn := func() (err error) { | ||
| defer Capture(&err, func() error { | ||
| return errClose | ||
| })() | ||
| return errOriginal | ||
| } | ||
|
|
||
| err := fn() | ||
| require.ErrorIs(t, err, errOriginal) | ||
| } | ||
|
|
||
| func TestCapturef(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
| errOriginal := stderrors.New("original error") | ||
|
|
||
| t.Run("nil fn is no-op", func(t *testing.T) { | ||
| var err error | ||
| fn := Capturef(&err, nil, "") | ||
| fn() | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("fn returns nil is no-op", func(t *testing.T) { | ||
| var err error | ||
| fn := Capturef(&err, func() error { return nil }, "") | ||
| fn() | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("nil rErr does not panic", func(t *testing.T) { | ||
| fn := Capturef(nil, func() error { return errClose }, "") | ||
| require.NotPanics(t, fn) | ||
| }) | ||
|
|
||
| t.Run("captures close error when original is nil and format is empty", func(t *testing.T) { | ||
| var err error | ||
| fn := Capturef(&err, func() error { return errClose }, "") | ||
| fn() | ||
| require.ErrorIs(t, err, errClose) | ||
| require.EqualError(t, err, "close error") | ||
| }) | ||
|
|
||
| t.Run("joins errors when both present and format is empty", func(t *testing.T) { | ||
| err := errOriginal | ||
| fn := Capturef(&err, func() error { return errClose }, "") | ||
| fn() | ||
| require.ErrorIs(t, err, errOriginal) | ||
| require.ErrorIs(t, err, errClose) | ||
| require.EqualError(t, err, "original error\nclose error") | ||
| }) | ||
|
|
||
| t.Run("preserves original when fn returns nil", func(t *testing.T) { | ||
| err := errOriginal | ||
| fn := Capturef(&err, func() error { return nil }, "") | ||
| fn() | ||
| require.ErrorIs(t, err, errOriginal) | ||
| }) | ||
|
|
||
| t.Run("wraps close error with format string", func(t *testing.T) { | ||
| var err error | ||
| errDisk := stderrors.New("no space left on device") | ||
| fn := Capturef(&err, func() error { return errDisk }, "error closing %q", "meta.boltdb") | ||
| fn() | ||
| require.ErrorIs(t, err, errDisk) | ||
| require.EqualError(t, err, `error closing "meta.boltdb": no space left on device`) | ||
| }) | ||
|
|
||
| t.Run("joins formatted close error with original", func(t *testing.T) { | ||
| err := errOriginal | ||
| errDisk := stderrors.New("no space left on device") | ||
| fn := Capturef(&err, func() error { return errDisk }, "error closing %q", "meta.boltdb") | ||
| fn() | ||
| require.ErrorIs(t, err, errOriginal) | ||
| require.ErrorIs(t, err, errDisk) | ||
| require.EqualError(t, err, "original error\nerror closing \"meta.boltdb\": no space left on device") | ||
| }) | ||
| } | ||
|
|
||
| func TestCapturef_DeferPattern(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
|
|
||
| fn := func() (err error) { | ||
| defer Capturef(&err, func() error { | ||
| return errClose | ||
| }, "closing resource")() | ||
| return nil | ||
| } | ||
|
|
||
| err := fn() | ||
| require.ErrorIs(t, err, errClose) | ||
| require.EqualError(t, err, "closing resource: close error") | ||
| } | ||
|
|
||
| func TestCapturef_DeferPatternJoinsErrors(t *testing.T) { | ||
| errClose := stderrors.New("close error") | ||
| errOriginal := stderrors.New("original error") | ||
|
|
||
| fn := func() (err error) { | ||
| defer Capturef(&err, func() error { | ||
| return errClose | ||
| }, "closing resource")() | ||
| return errOriginal | ||
| } | ||
|
|
||
| err := fn() | ||
| require.ErrorIs(t, err, errOriginal) | ||
| require.ErrorIs(t, err, errClose) | ||
| require.EqualError(t, err, "original error\nclosing resource: close error") | ||
| } | ||
|
|
||
| func TestCapturef_FormatStringDocExample(t *testing.T) { | ||
| errDisk := stderrors.New("no space left on device") | ||
|
|
||
| fn := func(name string) (rErr error) { | ||
| closer := func() error { return errDisk } | ||
| defer Capturef(&rErr, closer, "error closing %q", name)() | ||
| return nil | ||
| } | ||
|
|
||
| err := fn("meta.boltdb") | ||
| require.ErrorIs(t, err, errDisk) | ||
| require.EqualError(t, err, `error closing "meta.boltdb": no space left on device`) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Capturef doc comment has a few grammatical issues that make it harder to understand (e.g., “If an fn returns…”, “is creating”). Please reword for correctness/clarity so the generated GoDoc reads cleanly.