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
6 changes: 6 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (f *fundamental) Format(s fmt.State, verb rune) {
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
if f.stack != nil && len(*f.stack) > 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😐 if f.stack != nil then f.stack.Format is going to panic on line stack.go:147.

Good idea on the defensive programming, but it really ought to have been implemented different: build a if f.stack == nil { return } guard statement, which would even remove the existing bug that would cause this to panic.

But then, we obviously were never putting a nil *stack before, because we haven’t received reports of any crashes.

io.WriteString(s, "\n")
}
f.stack.Format(s, verb)
return
}
Expand Down Expand Up @@ -167,6 +170,9 @@ func (w *withStack) Format(s fmt.State, verb rune) {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
if w.stack != nil && len(*w.stack) > 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, wrong defensive programming pattern.

io.WriteString(s, "\n")
}
w.stack.Format(s, verb)
return
}
Expand Down
25 changes: 25 additions & 0 deletions leading_newline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package errors

import (
"fmt"
"strings"
"testing"
)

// Regression test for https://github.com/pkg/errors/issues/230:
// formatting a StackTrace directly with %+v must not produce a leading
// newline before the first frame.
func TestStackTraceFormatNoLeadingNewline(t *testing.T) {
st := stackTrace()[:2]
got := fmt.Sprintf("%+v", st)
if strings.HasPrefix(got, "\n") {
t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we want to avoid needing to escape quotes like this. In the first place, it looks kind of ugly, and in the second, using a raw string gives us a better idea of what the string should actually look like.

Suggested change
t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got)
t.Errorf(`fmt.Sprintf("%%+v", stackTrace) has leading newline:\n%q`, got)

}

// Frames must still be separated by newlines.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test as it appears here in code does not actually test this.

first := fmt.Sprintf("%+v", st[0])
both := fmt.Sprintf("%+v", st[:1])
if len(st) > 1 && !strings.Contains(got, first) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	if len(st) > 1 && !strings.Contains(got, first) {

What?😐 len(st) > 1 here is true by logical necessity.

t.Errorf("expected output to contain first frame %q, got %q", first, both)
}
}
13 changes: 9 additions & 4 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ func (st StackTrace) Format(s fmt.State, verb rune) {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
io.WriteString(s, "\n")
for i, f := range st {
if i > 0 {
io.WriteString(s, "\n")
}
f.Format(s, verb)
}
case s.Flag('#'):
Expand Down Expand Up @@ -144,9 +146,12 @@ func (s *stack) Format(st fmt.State, verb rune) {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
for i, pc := range *s {
if i > 0 {
io.WriteString(st, "\n")
}
f := Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
fmt.Fprintf(st, "%+v", f)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions stack_test.go
Original file line number Diff line number Diff line change
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 Down