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

Commit cbd18b5

Browse files
greensnarkdavecheney
authored andcommitted
Fix %q format for wrapped errors (#58)
Not handling the %q format causes logrus to log empty error strings for wrapped errors where x.Error() contains spaces or other quoteworthy characters. For reference, the logrus formatter does this: https://github.com/Sirupsen/logrus/blob/master/text_formatter.go#L154
1 parent 0bc61eb commit cbd18b5

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func (w wrapper) Format(s fmt.State, verb rune) {
152152
fallthrough
153153
case 's':
154154
io.WriteString(s, w.Error())
155+
case 'q':
156+
fmt.Fprintf(s, "%q", w.Error())
155157
}
156158
}
157159

format_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func TestFormatWrap(t *testing.T) {
8383
Wrap(io.EOF, "error"),
8484
"%s",
8585
"error: EOF",
86+
}, {
87+
Wrap(New("error with space"), "context"),
88+
"%q",
89+
`"context: error with space"`,
8690
}}
8791

8892
for _, tt := range tests {
@@ -109,7 +113,7 @@ func TestFormatWrapf(t *testing.T) {
109113
"EOF\n" +
110114
"error\n" +
111115
"github.com/pkg/errors.TestFormatWrapf\n" +
112-
"\t.+/github.com/pkg/errors/format_test.go:107",
116+
"\t.+/github.com/pkg/errors/format_test.go:111",
113117
}, {
114118
Wrapf(New("error"), "error%d", 2),
115119
"%v",
@@ -119,14 +123,14 @@ func TestFormatWrapf(t *testing.T) {
119123
"%+v",
120124
"error\n" +
121125
"github.com/pkg/errors.TestFormatWrapf\n" +
122-
"\t.+/github.com/pkg/errors/format_test.go:118",
126+
"\t.+/github.com/pkg/errors/format_test.go:122",
123127
}, {
124128
Wrap(Wrap(io.EOF, "error1"), "error2"),
125129
"%+v",
126130
"EOF\n" +
127131
"error1\n" +
128132
"github.com/pkg/errors.TestFormatWrapf\n" +
129-
"\t.+/github.com/pkg/errors/format_test.go:124\n",
133+
"\t.+/github.com/pkg/errors/format_test.go:128\n",
130134
}}
131135

132136
for _, tt := range tests {

0 commit comments

Comments
 (0)