Skip to content

Commit 5ffe365

Browse files
author
Dean Karn
authored
Merge pull request #5 from go-playground/wrap-skip-frames
New function: WrapSkipFrames
2 parents 29a69c6 + 33f4877 commit 5ffe365

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

chain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ type Tag struct {
1717
Value interface{}
1818
}
1919

20-
func newLink(err error, prefix string) *Link {
20+
func newLink(err error, prefix string, skipFrames int) *Link {
2121
return &Link{
2222
Err: err,
2323
Prefix: prefix,
24-
Source: st(),
24+
Source: st(skipFrames),
2525
}
2626

2727
}
@@ -109,5 +109,5 @@ func (c Chain) AddTypes(typ ...string) Chain {
109109

110110
// Wrap adds another contextual prefix to the error chain
111111
func (c Chain) Wrap(prefix string) Chain {
112-
return wrap(c, prefix)
112+
return wrap(c, prefix, 0)
113113
}

errors.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@ func RegisterHelper(helper Helper) {
1717

1818
// New creates an error with the provided text and automatically wraps it with line information.
1919
func New(s string) Chain {
20-
return wrap(errors.New(s), "")
20+
return wrap(errors.New(s), "", 0)
2121
}
2222

2323
// Wrap encapsulates the error, stores a contextual prefix and automatically obtains
2424
// a stack trace.
2525
func Wrap(err error, prefix string) Chain {
26-
return wrap(err, prefix)
26+
return wrap(err, prefix, 0)
2727
}
2828

29-
func wrap(err error, prefix string) (c Chain) {
29+
// WrapSkipFrames is a special version of Wrap that skips extra n frames when determining error location.
30+
// Normally only used when wrapping the library
31+
func WrapSkipFrames(err error, prefix string, n uint) Chain {
32+
return wrap(err, prefix, int(n))
33+
}
34+
35+
func wrap(err error, prefix string, skipFrames int) (c Chain) {
3036
var ok bool
3137
if c, ok = err.(Chain); ok {
32-
c = append(c, newLink(err, prefix))
38+
c = append(c, newLink(err, prefix, skipFrames))
3339
} else {
34-
c = Chain{newLink(err, prefix)}
40+
c = Chain{newLink(err, prefix, skipFrames)}
3541
}
3642
for _, h := range helpers {
3743
if !h(c, err) {

errors_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,39 @@ import (
99

1010
func TestWrap(t *testing.T) {
1111
defaultErr := fmt.Errorf("this is an %s", "error")
12+
testWrapper := func (err error, prefix string) Chain {
13+
return WrapSkipFrames(err, prefix, 1)
14+
}
15+
16+
err0 := New("42")
1217
err1 := Wrap(defaultErr, "prefix 1")
1318
err2 := err1.Wrap("prefix 2")
19+
err3 := testWrapper(err2, "prefix 3")
1420

1521
tests := []struct {
1622
err Chain
1723
pre string
1824
suf string
1925
}{
26+
{
27+
err: err0,
28+
pre: "TestWrap: ",
29+
suf: "errors_test.go:16",
30+
},
2031
{
2132
err: err1,
2233
pre: "TestWrap: ",
23-
suf: "errors_test.go:12",
34+
suf: "errors_test.go:17",
2435
},
2536
{
2637
err: err2,
2738
pre: "TestWrap: ",
28-
suf: "errors_test.go:13",
39+
suf: "errors_test.go:18",
40+
},
41+
{
42+
err: err3,
43+
pre: "TestWrap: ",
44+
suf: "errors_test.go:19",
2945
},
3046
}
3147

stack.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func funcname(name string) string {
112112
return name[i+1:]
113113
}
114114

115-
func st() string {
115+
func st(skipFrames int) string {
116116
s := callers()
117-
f := fr(s, 3)
117+
f := fr(s, 3 + skipFrames)
118118
name := fmt.Sprintf("%n", f)
119119
file := fmt.Sprintf("%+s", f)
120120
line := fmt.Sprintf("%d", f)

0 commit comments

Comments
 (0)