Skip to content

Commit

Permalink
full error return tracing implementation (like in Zig)
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Nov 7, 2024
1 parent 33e5472 commit 9d7a288
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 80 deletions.
10 changes: 10 additions & 0 deletions err2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,16 @@ func TestSetErrorTracer(t *testing.T) {
require.That(t, w == nil, "error tracer should be nil")
}

func TestSetErrRetTracer(t *testing.T) {
t.Parallel()
w := err2.ErrRetTracer()
require.That(t, w == nil, "error return tracer should be nil")
var w1 io.Writer
err2.SetErrRetTracer(w1)
w = err2.ErrRetTracer()
require.That(t, w == nil, "error return tracer should be nil")
}

func ExampleCatch_withFmt() {
// Set default logger to stdout for this example
oldLogW := err2.LogTracer()
Expand Down
19 changes: 15 additions & 4 deletions internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type StackInfo struct {

// these are used to filter out specific lines from output
ExlRegexp []*regexp.Regexp

PrintFirstOnly bool
}

var (
Expand All @@ -39,14 +41,19 @@ var (

// we want to check that this is not our package
packageRegexp = regexp.MustCompile(
`^github\.com/lainio/err2[a-zA-Z0-9_/.\[\]]*\(`,
`^github\.com/lainio/err2[a-zA-Z0-9_/\.\[\]\@]*\(`,
)

// testing package exluding regexps:
testingPkgRegexp = regexp.MustCompile(`^testing\.`)
testingFileRegexp = regexp.MustCompile(`^.*\/src\/testing\/testing\.go`)

exludeRegexps = []*regexp.Regexp{testingPkgRegexp, testingFileRegexp}
exludeRegexps = []*regexp.Regexp{testingPkgRegexp, testingFileRegexp}
exludeRegexpsAll = []*regexp.Regexp{
testingPkgRegexp,
testingFileRegexp,
packageRegexp,
}
)

func (si StackInfo) fullName() string {
Expand Down Expand Up @@ -89,7 +96,11 @@ func (si StackInfo) canPrint(s string, anchorLine, i int) (ok bool) {
// printed from call stack.
anchorLine = 0
}
ok = i >= 2*si.Level+anchorLine
if si.PrintFirstOnly {
ok = i >= 2*si.Level+anchorLine && i < 2*si.Level+anchorLine+2
} else {
ok = i >= 2*si.Level+anchorLine
}

if si.ExlRegexp == nil {
return ok
Expand Down Expand Up @@ -271,7 +282,7 @@ func stackPrint(r io.Reader, w io.Writer, si StackInfo) {
line := scanner.Text()

// we can print a line if we didn't find anything, i.e. anchorLine is
// nilAnchor, which means that our start is not limited by then anchor
// nilAnchor, which means that our start is not limited by the anchor
canPrint := anchorLine == nilAnchor
// if it's not nilAnchor we need to check it more carefully
if !canPrint {
Expand Down
Loading

0 comments on commit 9d7a288

Please sign in to comment.