Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit cc157cd

Browse files
committed
Store the stack of the caller to New, Errorf, Wrap and Wrapf (#27)
Store up to 32 stack frames for the caller to New, Errorf, Wrap, and Wrapf.
1 parent eeffa13 commit cc157cd

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ if err != nil {
1919
return errors.Wrap(err, "read failed")
2020
}
2121
```
22-
In addition, `errors.Wrap` records the file and line where it was called, allowing the programmer to retrieve the path to the original error.
22+
## Retrieving the stack trace of an error or wrapper
2323

24+
`New`, `Errorf`, `Wrap`, and `Wrapf` record a stack trace at the point they are invoked.
25+
This information can be retrieved with the following interface.
26+
```go
27+
type Stack interface {
28+
Stack() []uintptr
29+
}
30+
```
2431
## Retrieving the cause of an error
2532

2633
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to recurse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
2734
```go
2835
type causer interface {
29-
Cause() error
36+
Cause() error
3037
}
3138
```
3239
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:

errors.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
// return errors.Wrap(err, "read failed")
2222
// }
2323
//
24-
// In addition, errors.Wrap records the file and line where it was called,
25-
// allowing the programmer to retrieve the path to the original error.
24+
// Retrieving the stack trace of an error or wrapper
25+
//
26+
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
27+
// invoked. This information can be retrieved with the following interface.
28+
//
29+
// type Stack interface {
30+
// Stack() []uintptr
31+
// }
2632
//
2733
// Retrieving the cause of an error
2834
//
@@ -31,8 +37,8 @@
3137
// to reverse the operation of errors.Wrap to retrieve the original error
3238
// for inspection. Any error value which implements this interface
3339
//
34-
// type causer interface {
35-
// Cause() error
40+
// type Causer interface {
41+
// Cause() error
3642
// }
3743
//
3844
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
@@ -58,6 +64,8 @@ import (
5864
// stack represents a stack of programm counters.
5965
type stack []uintptr
6066

67+
func (s stack) Stack() []uintptr { return s }
68+
6169
func (s stack) Location() (string, int) {
6270
return location(s[0] - 1)
6371
}
@@ -191,7 +199,7 @@ func Fprint(w io.Writer, err error) {
191199
}
192200

193201
func callers() stack {
194-
const depth = 1
202+
const depth = 32
195203
var pcs [depth]uintptr
196204
n := runtime.Callers(3, pcs[:])
197205
return pcs[0:n]

errors_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,42 @@ func TestErrorf(t *testing.T) {
187187
}
188188
}
189189
}
190+
191+
func TestStack(t *testing.T) {
192+
type fileline struct {
193+
file string
194+
line int
195+
}
196+
tests := []struct {
197+
err error
198+
want []fileline
199+
}{{
200+
New("ooh"), []fileline{
201+
{"github.com/pkg/errors/errors_test.go", 200},
202+
},
203+
}, {
204+
Wrap(New("ooh"), "ahh"), []fileline{
205+
{"github.com/pkg/errors/errors_test.go", 204}, // this is the stack of Wrap, not New
206+
},
207+
}, {
208+
Cause(Wrap(New("ooh"), "ahh")), []fileline{
209+
{"github.com/pkg/errors/errors_test.go", 208}, // this is the stack of New
210+
},
211+
}}
212+
for _, tt := range tests {
213+
x, ok := tt.err.(interface {
214+
Stack() []uintptr
215+
})
216+
if !ok {
217+
t.Errorf("expected %#v to implement Stack()", tt.err)
218+
continue
219+
}
220+
st := x.Stack()
221+
for i, want := range tt.want {
222+
file, line := location(st[i] - 1)
223+
if file != want.file || line != want.line {
224+
t.Errorf("frame %d: expected %s:%d, got %s:%d", i, want.file, want.line, file, line)
225+
}
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)