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

Commit 17b591d

Browse files
ncwdavecheney
authored andcommitted
Fix the %q format for errors so it puts "" around the output (#83)
1 parent a221380 commit 17b591d

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ func (f *fundamental) Format(s fmt.State, verb rune) {
127127
return
128128
}
129129
fallthrough
130-
case 's', 'q':
130+
case 's':
131131
io.WriteString(s, f.msg)
132+
case 'q':
133+
fmt.Fprintf(s, "%q", f.msg)
132134
}
133135
}
134136

format_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func TestFormatNew(t *testing.T) {
2727
"error\n" +
2828
"github.com/pkg/errors.TestFormatNew\n" +
2929
"\t.+/github.com/pkg/errors/format_test.go:25",
30+
}, {
31+
New("error"),
32+
"%q",
33+
`"error"`,
3034
}}
3135

3236
for i, tt := range tests {
@@ -52,7 +56,7 @@ func TestFormatErrorf(t *testing.T) {
5256
"%+v",
5357
"error\n" +
5458
"github.com/pkg/errors.TestFormatErrorf\n" +
55-
"\t.+/github.com/pkg/errors/format_test.go:51",
59+
"\t.+/github.com/pkg/errors/format_test.go:55",
5660
}}
5761

5862
for i, tt := range tests {
@@ -78,7 +82,7 @@ func TestFormatWrap(t *testing.T) {
7882
"%+v",
7983
"error\n" +
8084
"github.com/pkg/errors.TestFormatWrap\n" +
81-
"\t.+/github.com/pkg/errors/format_test.go:77",
85+
"\t.+/github.com/pkg/errors/format_test.go:81",
8286
}, {
8387
Wrap(io.EOF, "error"),
8488
"%s",
@@ -93,14 +97,14 @@ func TestFormatWrap(t *testing.T) {
9397
"EOF\n" +
9498
"error\n" +
9599
"github.com/pkg/errors.TestFormatWrap\n" +
96-
"\t.+/github.com/pkg/errors/format_test.go:91",
100+
"\t.+/github.com/pkg/errors/format_test.go:95",
97101
}, {
98102
Wrap(Wrap(io.EOF, "error1"), "error2"),
99103
"%+v",
100104
"EOF\n" +
101105
"error1\n" +
102106
"github.com/pkg/errors.TestFormatWrap\n" +
103-
"\t.+/github.com/pkg/errors/format_test.go:98\n",
107+
"\t.+/github.com/pkg/errors/format_test.go:102\n",
104108
}, {
105109
Wrap(New("error with space"), "context"),
106110
"%q",
@@ -131,7 +135,7 @@ func TestFormatWrapf(t *testing.T) {
131135
"EOF\n" +
132136
"error2\n" +
133137
"github.com/pkg/errors.TestFormatWrapf\n" +
134-
"\t.+/github.com/pkg/errors/format_test.go:129",
138+
"\t.+/github.com/pkg/errors/format_test.go:133",
135139
}, {
136140
Wrapf(New("error"), "error%d", 2),
137141
"%s",
@@ -145,7 +149,7 @@ func TestFormatWrapf(t *testing.T) {
145149
"%+v",
146150
"error\n" +
147151
"github.com/pkg/errors.TestFormatWrapf\n" +
148-
"\t.+/github.com/pkg/errors/format_test.go:144",
152+
"\t.+/github.com/pkg/errors/format_test.go:148",
149153
}}
150154

151155
for i, tt := range tests {

0 commit comments

Comments
 (0)