Repository navigation
Fix leading newline in StackTrace %+v output #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
| io.WriteString(s, "\n") | ||
| } | ||
| f.stack.Format(s, verb) | ||
| return | ||
| } | ||
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
|
|
||
| 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) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
| } | ||||||
|
|
||||||
| // Frames must still be separated by newlines. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if len(st) > 1 && !strings.Contains(got, first) {What?😐 |
||||||
| t.Errorf("expected output to contain first frame %q, got %q", first, both) | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😐 if
f.stack != nilthenf.stack.Formatis going to panic on linestack.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*stackbefore, because we haven’t received reports of any crashes.