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

Commit 72fa05e

Browse files
randall77davecheney
authored andcommitted
errors: detect unknown frames correctly (#192)
With Go 1.12, we will now be doing mid-stack inlining. This exposes inlined frames to runtime.Callers and friends. The spec says that a runtime.Frame may have a nil Func field for inlined functions. Instead, use the Function field to detect whether we really have an empty Frame.
1 parent c38ea53 commit 72fa05e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

format_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,30 @@ func TestFormatGeneric(t *testing.T) {
360360
}
361361
}
362362

363+
func wrappedNew(message string) error { // This function will be mid-stack inlined in go 1.12+
364+
return New(message)
365+
}
366+
367+
func TestFormatWrappedNew(t *testing.T) {
368+
tests := []struct {
369+
error
370+
format string
371+
want string
372+
}{{
373+
wrappedNew("error"),
374+
"%+v",
375+
"error\n" +
376+
"github.com/pkg/errors.wrappedNew\n" +
377+
"\t.+/github.com/pkg/errors/format_test.go:364\n" +
378+
"github.com/pkg/errors.TestFormatWrappedNew\n" +
379+
"\t.+/github.com/pkg/errors/format_test.go:373",
380+
}}
381+
382+
for i, tt := range tests {
383+
testFormatRegexp(t, i, tt.error, tt.format, tt.want)
384+
}
385+
}
386+
363387
func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
364388
got := fmt.Sprintf(format, arg)
365389
gotLines := strings.SplitN(got, "\n", -1)

stack.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ func (f Frame) format(w io.Writer, s fmt.State, verb rune) {
3535
case 's':
3636
switch {
3737
case s.Flag('+'):
38-
fn := f.Func
39-
if fn == nil {
38+
if f.Function == "" {
4039
io.WriteString(w, "unknown")
4140
} else {
42-
io.WriteString(w, fn.Name())
41+
io.WriteString(w, f.Function)
4342
io.WriteString(w, "\n\t")
4443
io.WriteString(w, f.File)
4544
}

0 commit comments

Comments
 (0)