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

Commit

Permalink
Fix the %q format for errors so it puts "" around the output (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw authored and davecheney committed Aug 22, 2016
1 parent a221380 commit 17b591d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ func (f *fundamental) Format(s fmt.State, verb rune) {
return
}
fallthrough
case 's', 'q':
case 's':
io.WriteString(s, f.msg)
case 'q':
fmt.Fprintf(s, "%q", f.msg)
}
}

Expand Down
16 changes: 10 additions & 6 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestFormatNew(t *testing.T) {
"error\n" +
"github.com/pkg/errors.TestFormatNew\n" +
"\t.+/github.com/pkg/errors/format_test.go:25",
}, {
New("error"),
"%q",
`"error"`,
}}

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

for i, tt := range tests {
Expand All @@ -78,7 +82,7 @@ func TestFormatWrap(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatWrap\n" +
"\t.+/github.com/pkg/errors/format_test.go:77",
"\t.+/github.com/pkg/errors/format_test.go:81",
}, {
Wrap(io.EOF, "error"),
"%s",
Expand All @@ -93,14 +97,14 @@ func TestFormatWrap(t *testing.T) {
"EOF\n" +
"error\n" +
"github.com/pkg/errors.TestFormatWrap\n" +
"\t.+/github.com/pkg/errors/format_test.go:91",
"\t.+/github.com/pkg/errors/format_test.go:95",
}, {
Wrap(Wrap(io.EOF, "error1"), "error2"),
"%+v",
"EOF\n" +
"error1\n" +
"github.com/pkg/errors.TestFormatWrap\n" +
"\t.+/github.com/pkg/errors/format_test.go:98\n",
"\t.+/github.com/pkg/errors/format_test.go:102\n",
}, {
Wrap(New("error with space"), "context"),
"%q",
Expand Down Expand Up @@ -131,7 +135,7 @@ func TestFormatWrapf(t *testing.T) {
"EOF\n" +
"error2\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
"\t.+/github.com/pkg/errors/format_test.go:129",
"\t.+/github.com/pkg/errors/format_test.go:133",
}, {
Wrapf(New("error"), "error%d", 2),
"%s",
Expand All @@ -145,7 +149,7 @@ func TestFormatWrapf(t *testing.T) {
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
"\t.+/github.com/pkg/errors/format_test.go:144",
"\t.+/github.com/pkg/errors/format_test.go:148",
}}

for i, tt := range tests {
Expand Down

2 comments on commit 17b591d

@AlekSi
Copy link
Contributor

@AlekSi AlekSi commented on 17b591d Aug 22, 2016

Choose a reason for hiding this comment

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

Documentation was not updated.

@davecheney
Copy link
Member

@davecheney davecheney commented on 17b591d Aug 23, 2016 via email

Choose a reason for hiding this comment

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

Please sign in to comment.