Skip to content
Open
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
10 changes: 8 additions & 2 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ func (st StackTrace) Format(s fmt.State, verb rune) {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
io.WriteString(s, "\n")
// Separate frames with newlines, but do not prefix the first frame
// with a leading newline so "%+v" on a StackTrace alone is usable
// without an initial blank line (#230). Error formatting still
// inserts a separator via stack.Format after the error message.
for i, f := range st {
if i > 0 {
io.WriteString(s, "\n")
}
f.Format(s, verb)
}
case s.Flag('#'):
Expand Down
18 changes: 13 additions & 5 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func TestStackTrace(t *testing.T) {
return Errorf("hello %s", fmt.Sprintf("world: %s", "ooh"))
}()
}()), []string{
`github.com/pkg/errors.TestStackTrace.func2.1` +
`github.com/pkg/errors.TestStackTrace\..*` +
"\n\t.+/github.com/pkg/errors/stack_test.go:145", // this is the stack of Errorf
`github.com/pkg/errors.TestStackTrace.func2` +
`github.com/pkg/errors.TestStackTrace\..*` +
"\n\t.+/github.com/pkg/errors/stack_test.go:146", // this is the stack of Errorf's caller
"github.com/pkg/errors.TestStackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:147", // this is the stack of Errorf's caller's caller
Expand Down Expand Up @@ -224,15 +224,14 @@ func TestStackTraceFormat(t *testing.T) {
}, {
stackTrace()[:2],
"%+v",
"\n" +
"github.com/pkg/errors.stackTrace\n" +
"github.com/pkg/errors.stackTrace\n" +
"\t.+/github.com/pkg/errors/stack_test.go:174\n" +
"github.com/pkg/errors.TestStackTraceFormat\n" +
"\t.+/github.com/pkg/errors/stack_test.go:225",
}, {
stackTrace()[:2],
"%#v",
`\[\]errors.Frame{stack_test.go:174, stack_test.go:233}`,
`\[\]errors.Frame{stack_test.go:174, stack_test.go:232}`,
}}

for i, tt := range tests {
Expand All @@ -248,3 +247,12 @@ func caller() Frame {
frame, _ := frames.Next()
return Frame(frame.PC)
}


func TestStackTraceFormatNoLeadingNewline(t *testing.T) {
st := stackTrace()[:1]
got := fmt.Sprintf("%+v", st)
if len(got) > 0 && got[0] == '\n' {
t.Fatalf("StackTrace has leading newline: %q", got)
}
}