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

Rename Stacktrace to StackTrace #51

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@
// %s print the error. If the error has a Cause it will be
// printed recursively
// %v see %s
// %+v extended format. Each Frame of the error's Stacktrace will
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// 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 Stacktrace interface {
// Stacktrace() errors.Stacktrace
// type StackTrace interface {
// StackTrace() errors.StackTrace
// }
//
// Where errors.Stacktrace is defined as
// Where errors.StackTrace is defined as
//
// type Stacktrace []Frame
// type StackTrace []Frame
//
// The Frame type represents a call site in the stacktrace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stacktrace of this error. For example:
//
// if err, ok := err.(Stacktrace); ok {
// for _, f := range err.Stacktrace() {
// if err, ok := err.(StackTrace); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d", f)
// }
// }
Expand All @@ -99,7 +99,7 @@ func (e _error) Format(s fmt.State, verb rune) {
case 'v':
if s.Flag('+') {
io.WriteString(s, e.msg)
fmt.Fprintf(s, "%+v", e.Stacktrace())
fmt.Fprintf(s, "%+v", e.StackTrace())
return
}
fallthrough
Expand Down Expand Up @@ -145,7 +145,7 @@ func (w wrapper) Format(s fmt.State, verb rune) {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", w.Cause())
fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
fmt.Fprintf(s, "%+v: %s", w.StackTrace()[0], w.msg)
return
}
fallthrough
Expand Down
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ func ExampleErrorf_extended() {
}

func Example_stacktrace() {
type Stacktrace interface {
Stacktrace() errors.Stacktrace
type StackTrace interface {
StackTrace() errors.StackTrace
}

err, ok := errors.Cause(fn()).(Stacktrace)
err, ok := errors.Cause(fn()).(StackTrace)
if !ok {
panic("oops, err does not implement Stacktrace")
panic("oops, err does not implement StackTrace")
}

st := err.Stacktrace()
st := err.StackTrace()
fmt.Printf("%+v", st[0:2]) // top two frames

// Example output:
Expand Down
8 changes: 4 additions & 4 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func (f Frame) Format(s fmt.State, verb rune) {
}
}

// Stacktrace is stack of Frames from innermost (newest) to outermost (oldest).
type Stacktrace []Frame
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame

func (st Stacktrace) Format(s fmt.State, verb rune) {
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
Expand All @@ -100,7 +100,7 @@ func (st Stacktrace) Format(s fmt.State, verb rune) {
// stack represents a stack of program counters.
type stack []uintptr

func (s *stack) Stacktrace() Stacktrace {
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
Expand Down
44 changes: 22 additions & 22 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,30 @@ func TestTrimGOPATH(t *testing.T) {
}
}

func TestStacktrace(t *testing.T) {
func TestStackTrace(t *testing.T) {
tests := []struct {
err error
want []string
}{{
New("ooh"), []string{
"github.com/pkg/errors.TestStacktrace\n" +
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:175",
},
}, {
Wrap(New("ooh"), "ahh"), []string{
"github.com/pkg/errors.TestStacktrace\n" +
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:180", // this is the stack of Wrap, not New
},
}, {
Cause(Wrap(New("ooh"), "ahh")), []string{
"github.com/pkg/errors.TestStacktrace\n" +
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:185", // this is the stack of New
},
}, {
func() error { return New("ooh") }(), []string{
`github.com/pkg/errors.(func·005|TestStacktrace.func1)` +
`github.com/pkg/errors.(func·005|TestStackTrace.func1)` +
"\n\t.+/github.com/pkg/errors/stack_test.go:190", // this is the stack of New
"github.com/pkg/errors.TestStacktrace\n" +
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:190", // this is the stack of New's caller
},
}, {
Expand All @@ -199,40 +199,40 @@ func TestStacktrace(t *testing.T) {
return Errorf("hello %s", fmt.Sprintf("world"))
}()
}()), []string{
`github.com/pkg/errors.(func·006|TestStacktrace.func2.1)` +
`github.com/pkg/errors.(func·006|TestStackTrace.func2.1)` +
"\n\t.+/github.com/pkg/errors/stack_test.go:199", // this is the stack of Errorf
`github.com/pkg/errors.(func·007|TestStacktrace.func2)` +
`github.com/pkg/errors.(func·007|TestStackTrace.func2)` +
"\n\t.+/github.com/pkg/errors/stack_test.go:200", // this is the stack of Errorf's caller
"github.com/pkg/errors.TestStacktrace\n" +
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:201", // this is the stack of Errorf's caller's caller
},
}}
for _, tt := range tests {
x, ok := tt.err.(interface {
Stacktrace() Stacktrace
StackTrace() StackTrace
})
if !ok {
t.Errorf("expected %#v to implement Stacktrace() Stacktrace", tt.err)
t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err)
continue
}
st := x.Stacktrace()
st := x.StackTrace()
for j, want := range tt.want {
testFormatRegexp(t, st[j], "%+v", want)
}
}
}

func stacktrace() Stacktrace {
func stacktrace() StackTrace {
const depth = 8
var pcs [depth]uintptr
n := runtime.Callers(1, pcs[:])
var st stack = pcs[0:n]
return st.Stacktrace()
return st.StackTrace()
}

func TestStacktraceFormat(t *testing.T) {
func TestStackTraceFormat(t *testing.T) {
tests := []struct {
Stacktrace
StackTrace
format string
want string
}{{
Expand All @@ -252,19 +252,19 @@ func TestStacktraceFormat(t *testing.T) {
"%#v",
`\[\]errors.Frame\(nil\)`,
}, {
make(Stacktrace, 0),
make(StackTrace, 0),
"%s",
`\[\]`,
}, {
make(Stacktrace, 0),
make(StackTrace, 0),
"%v",
`\[\]`,
}, {
make(Stacktrace, 0),
make(StackTrace, 0),
"%+v",
"",
}, {
make(Stacktrace, 0),
make(StackTrace, 0),
"%#v",
`\[\]errors.Frame{}`,
}, {
Expand All @@ -281,7 +281,7 @@ func TestStacktraceFormat(t *testing.T) {
"\n" +
"github.com/pkg/errors.stacktrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:228\n" +
"github.com/pkg/errors.TestStacktraceFormat\n" +
"github.com/pkg/errors.TestStackTraceFormat\n" +
"\t.+/github.com/pkg/errors/stack_test.go:279",
}, {
stacktrace()[:2],
Expand All @@ -290,6 +290,6 @@ func TestStacktraceFormat(t *testing.T) {
}}

for _, tt := range tests {
testFormatRegexp(t, tt.Stacktrace, tt.format, tt.want)
testFormatRegexp(t, tt.StackTrace, tt.format, tt.want)
}
}