diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 677666f..40d995a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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: diff --git a/ctxerr.go b/ctxerr.go index 1805445..de1d75e 100644 --- a/ctxerr.go +++ b/ctxerr.go @@ -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 { diff --git a/ctxerr_test.go b/ctxerr_test.go index 53a91c5..c758172 100644 --- a/ctxerr_test.go +++ b/ctxerr_test.go @@ -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 diff --git a/go.mod b/go.mod index 67e4bf7..e59a3d6 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/mvndaai/ctxerr -go 1.20 +go 1.22