Skip to content

Commit

Permalink
add CallerFuncs helper function (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvndaai authored Sep 24, 2024
1 parent e849e02 commit 22d6240
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
go-version: [1.22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
9 changes: 9 additions & 0 deletions ctxerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ func CallerFunc(skip int) string {
return f
}

// CallerFuncs is a shortcut for calling CallerFunc many times
func CallerFuncs(skip, depth int) []string {
f := []string{}
for i := 0; i < depth; i++ {
f = append(f, CallerFunc(skip+i+1))
}
return f
}

// AllFields unwraps the error collecting/replacing fields as it goes down the tree
func AllFields(err error) map[string]any { return global.AllFields(err) }
func (in Instance) AllFields(err error) map[string]any {
Expand Down
25 changes: 25 additions & 0 deletions ctxerr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ func TestCallerFunc(t *testing.T) {
}
}

func testCallerA(skip, depth int) []string { return ctxerr.CallerFuncs(skip, depth) }
func testCallerB(skip, depth int) []string { return testCallerA(skip, depth) }
func testCallerC(skip, depth int) []string { return testCallerB(skip, depth) }

func TestCallerFuncs(t *testing.T) {
fns := testCallerC(0, 3)
expected := []string{
"ctxerr_test.testCallerA",
"ctxerr_test.testCallerB",
"ctxerr_test.testCallerC",
}
if fmt.Sprint(fns) != fmt.Sprint(expected) {
t.Error("Did not match expected", fns, expected)
}

fns = testCallerC(1, 2)
expected = []string{
"ctxerr_test.testCallerB",
"ctxerr_test.testCallerC",
}
if fmt.Sprint(fns) != fmt.Sprint(expected) {
t.Error("Did not match expected", fns, expected)
}
}

func TestLocation(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/mvndaai/ctxerr

go 1.20
go 1.22

0 comments on commit 22d6240

Please sign in to comment.