Repository navigation
Add GetCaller for use in error helpers #108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package errtrace_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "braces.dev/errtrace" | ||
| "braces.dev/errtrace/internal/tracetest" | ||
| ) | ||
|
|
||
| func f1Wrap() error { | ||
| return wrap(f2Wrap(), "u=1") | ||
| } | ||
|
|
||
| func f2Wrap() error { | ||
| return wrap(f3Wrap(), "method=order") | ||
| } | ||
|
|
||
| func f3Wrap() error { | ||
| return wrap(errors.New("failed"), "foo") | ||
| } | ||
|
|
||
| func wrap(err error, fields ...string) error { | ||
| return errtrace.GetCaller(). | ||
| Wrap(fmt.Errorf("%w %v", err, fields)) | ||
| } | ||
|
|
||
| func Example_getCaller() { | ||
| got := errtrace.FormatString(f1Wrap()) | ||
|
|
||
| // make trace agnostic to environment-specific location | ||
| // and less sensitive to line number changes. | ||
| fmt.Println(tracetest.MustClean(got)) | ||
|
|
||
| // Output: | ||
| //failed [foo] [method=order] [u=1] | ||
| // | ||
| //braces.dev/errtrace_test.f3Wrap | ||
| // /path/to/errtrace/example_errhelper_test.go:3 | ||
| //braces.dev/errtrace_test.f2Wrap | ||
| // /path/to/errtrace/example_errhelper_test.go:2 | ||
| //braces.dev/errtrace_test.f1Wrap | ||
| // /path/to/errtrace/example_errhelper_test.go:1 | ||
| } |
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
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
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,33 @@ | ||
| package errtrace | ||
|
|
||
| import "braces.dev/errtrace/internal/pc" | ||
|
|
||
| // Caller represents a single caller frame, and is intended for error helpers | ||
| // to capture caller information for wrapping. See [GetCaller] for details. | ||
| type Caller struct { | ||
| callerPC uintptr | ||
| } | ||
|
|
||
| // GetCaller captures the program counter of a caller, primarily intended for | ||
| // error helpers so caller information captures the helper's caller. | ||
| // | ||
| // Callers of this function should be marked '//go:noinline' to avoid inlining, | ||
| // as GetCaller expects to skip the caller's stack frame. | ||
| // | ||
| // //go:noinline | ||
| // func Wrapf(err error, msg string, args ...any) { | ||
| // caller := errtrace.GetCaller() | ||
| // err := ... | ||
| // return caller.Wrap(err) | ||
| // } | ||
| // | ||
| //go:noinline | ||
| func GetCaller() Caller { | ||
| return Caller{pc.GetCallerSkip1()} | ||
| } | ||
|
|
||
| // Wrap adds the program counter captured in Caller to the error, | ||
| // similar to [Wrap], but relying on previously captured caller inforamtion. | ||
| func (c Caller) Wrap(err error) error { | ||
| return wrap(err, c.callerPC) | ||
| } | ||
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,10 @@ | ||
| //go:build safe || !(amd64 || arm64) | ||
|
|
||
| // | ||
| // Build tag must match pc_safe.go | ||
|
|
||
| package errtrace_test | ||
|
|
||
| func init() { | ||
| safe = true | ||
| } |
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,113 @@ | ||
| package errtrace_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "braces.dev/errtrace" | ||
| ) | ||
|
|
||
| var safe = false | ||
|
|
||
| // Note: Though test tables could DRY up the below tests, they're intentionally | ||
| // kept as functions calling other simple functions to test how inlining impacts | ||
| // use of `GetCaller`. | ||
|
|
||
| func TestGetCallerWrap_ErrorsNew(t *testing.T) { | ||
| err := callErrorsNew() | ||
| wantErr(t, err, "callErrorsNew") | ||
| } | ||
|
|
||
| func callErrorsNew() error { | ||
| return errorsNew("wrap errors.New") | ||
| } | ||
|
|
||
| func errorsNew(msg string) error { | ||
| caller := errtrace.GetCaller() | ||
| return caller.Wrap(errors.New(msg)) | ||
| } | ||
|
|
||
| func TestGetCallerWrap_WrapExisting(t *testing.T) { | ||
| err := callWrapExisting() | ||
| wantErr(t, err, "callWrapExisting") | ||
| } | ||
|
|
||
| func callWrapExisting() error { | ||
| return wrapExisting() | ||
| } | ||
|
|
||
| var errFoo = errors.New("foo") | ||
|
|
||
| func wrapExisting() error { | ||
| return errtrace.GetCaller().Wrap(errFoo) | ||
| } | ||
|
|
||
| func TestGetCallerWrap_PassCaller(t *testing.T) { | ||
| err := callPassCaller() | ||
| wantErr(t, err, "callPassCaller") | ||
| } | ||
|
|
||
| func callPassCaller() error { | ||
| return passCaller() | ||
| } | ||
|
|
||
| func passCaller() error { | ||
| return passCallerInner(errtrace.GetCaller()) | ||
| } | ||
|
|
||
| func passCallerInner(caller errtrace.Caller) error { | ||
| return caller.Wrap(errFoo) | ||
| } | ||
|
|
||
| func TestGetCallerWrap_RetCaller(t *testing.T) { | ||
| err := callRetCaller() | ||
|
|
||
| wantFn := "callRetCaller" | ||
| if !safe { | ||
| // If the function calling pc.GetCaller is inlined, there's no stack frame | ||
| // so the assembly implementation can skip the correct caller. | ||
| // Callers of GetCaller using `go:noinline` avoid this (as recommended in the docs). | ||
| // Inlining is not consistent, hence we check the frame in !safe mode. | ||
| f, _, _ := errtrace.UnwrapFrame(err) | ||
| if !strings.HasSuffix(f.Function, wantFn) { | ||
| wantFn = "TestGetCallerWrap_RetCaller" | ||
| } | ||
| } | ||
| wantErr(t, err, wantFn) | ||
| } | ||
|
|
||
| func callRetCaller() error { | ||
| return retCaller().Wrap(errFoo) | ||
| } | ||
|
|
||
| func retCaller() errtrace.Caller { | ||
| return errtrace.GetCaller() | ||
| } | ||
|
|
||
| func TestGetCallerWrap_RetCallerNoInline(t *testing.T) { | ||
| err := callRetCallerNoInline() | ||
| wantErr(t, err, "callRetCallerNoInline") | ||
| } | ||
|
|
||
| func callRetCallerNoInline() error { | ||
| return retCallerNoInline().Wrap(errFoo) | ||
| } | ||
|
|
||
| //go:noinline | ||
| func retCallerNoInline() errtrace.Caller { | ||
| return errtrace.GetCaller() | ||
| } | ||
|
|
||
| func wantErr(t testing.TB, err error, fn string) runtime.Frame { | ||
| if err == nil { | ||
| t.Fatalf("expected err") | ||
| } | ||
|
|
||
| f, _, _ := errtrace.UnwrapFrame(err) | ||
| if !strings.HasSuffix(f.Function, "."+fn) { | ||
| t.Errorf("expected caller to be %v, got %v", fn, f.Function) | ||
| } | ||
| return f | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.