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

Commit

Permalink
Store the stack of the caller to New, Errorf, Wrap and Wrapf
Browse files Browse the repository at this point in the history
Store up to 32 stack frames for the caller to New, Errorf, Wrap, and
Wrapf.
  • Loading branch information
davecheney committed May 23, 2016
1 parent eeffa13 commit 701c991
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ if err != nil {
return errors.Wrap(err, "read failed")
}
```
In addition, `errors.Wrap` records the file and line where it was called, allowing the programmer to retrieve the path to the original error.
## Retrieving the stack trace of an error or wrapper

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

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`.
```go
type causer interface {
Cause() error
Cause() error
}
```
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
Expand Down
18 changes: 13 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
// return errors.Wrap(err, "read failed")
// }
//
// In addition, errors.Wrap records the file and line where it was called,
// allowing the programmer to retrieve the path to the original error.
// Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface.
//
// type Stack interface {
// Stack() []uintptr
// }
//
// Retrieving the cause of an error
//
Expand All @@ -31,8 +37,8 @@
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// type Causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
Expand All @@ -58,6 +64,8 @@ import (
// stack represents a stack of programm counters.
type stack []uintptr

func (s stack) Stack() []uintptr { return s }

func (s stack) Location() (string, int) {
return location(s[0] - 1)
}
Expand Down Expand Up @@ -191,7 +199,7 @@ func Fprint(w io.Writer, err error) {
}

func callers() stack {
const depth = 1
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
return pcs[0:n]
Expand Down
32 changes: 32 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,35 @@ func TestErrorf(t *testing.T) {
}
}
}

func TestStack(t *testing.T) {
type fileline struct {
file string
line int
}
tests := []struct {
err error
want []fileline
}{{
New("ooh"), []fileline{
{"github.com/pkg/errors/errors_test.go", 200},
{"testing/testing.go", 547},
},
}}
for _, tt := range tests {
x, ok := tt.err.(interface {
Stack() []uintptr
})
if !ok {
t.Errorf("expected %#v to implement Stack()", tt.err)
continue
}
st := x.Stack()
for i, want := range tt.want {
file, line := location(st[i] - 1)
if file != want.file || line != want.line {
t.Errorf("frame %d: expected %s:%d, got %s:%d", i, want.file, want.line, file, line)
}
}
}
}

0 comments on commit 701c991

Please sign in to comment.