Skip to content

Commit 9d7a288

Browse files
committed
full error return tracing implementation (like in Zig)
1 parent 33e5472 commit 9d7a288

File tree

7 files changed

+237
-80
lines changed

7 files changed

+237
-80
lines changed

err2_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,16 @@ func TestSetErrorTracer(t *testing.T) {
672672
require.That(t, w == nil, "error tracer should be nil")
673673
}
674674

675+
func TestSetErrRetTracer(t *testing.T) {
676+
t.Parallel()
677+
w := err2.ErrRetTracer()
678+
require.That(t, w == nil, "error return tracer should be nil")
679+
var w1 io.Writer
680+
err2.SetErrRetTracer(w1)
681+
w = err2.ErrRetTracer()
682+
require.That(t, w == nil, "error return tracer should be nil")
683+
}
684+
675685
func ExampleCatch_withFmt() {
676686
// Set default logger to stdout for this example
677687
oldLogW := err2.LogTracer()

internal/debug/debug.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type StackInfo struct {
2626

2727
// these are used to filter out specific lines from output
2828
ExlRegexp []*regexp.Regexp
29+
30+
PrintFirstOnly bool
2931
}
3032

3133
var (
@@ -39,14 +41,19 @@ var (
3941

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

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

49-
exludeRegexps = []*regexp.Regexp{testingPkgRegexp, testingFileRegexp}
51+
exludeRegexps = []*regexp.Regexp{testingPkgRegexp, testingFileRegexp}
52+
exludeRegexpsAll = []*regexp.Regexp{
53+
testingPkgRegexp,
54+
testingFileRegexp,
55+
packageRegexp,
56+
}
5057
)
5158

5259
func (si StackInfo) fullName() string {
@@ -89,7 +96,11 @@ func (si StackInfo) canPrint(s string, anchorLine, i int) (ok bool) {
8996
// printed from call stack.
9097
anchorLine = 0
9198
}
92-
ok = i >= 2*si.Level+anchorLine
99+
if si.PrintFirstOnly {
100+
ok = i >= 2*si.Level+anchorLine && i < 2*si.Level+anchorLine+2
101+
} else {
102+
ok = i >= 2*si.Level+anchorLine
103+
}
93104

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

273284
// we can print a line if we didn't find anything, i.e. anchorLine is
274-
// nilAnchor, which means that our start is not limited by then anchor
285+
// nilAnchor, which means that our start is not limited by the anchor
275286
canPrint := anchorLine == nilAnchor
276287
// if it's not nilAnchor we need to check it more carefully
277288
if !canPrint {

0 commit comments

Comments
 (0)