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
diff --git a/errors.go b/errors.go
index 216b768..053d9d3 100644
--- a/errors.go
+++ b/errors.go
@@ -152,6 +152,8 @@
fallthrough
case 's':
io.WriteString(s, w.Error())
+ case 'q':
+ fmt.Fprintf(s, "%q", w.Error())
}
}
diff --git a/format_test.go b/format_test.go
index a862087..d1b2f1b 100644
--- a/format_test.go
+++ b/format_test.go
@@ -83,6 +83,10 @@
Wrap(io.EOF, "error"),
"%s",
"error: EOF",
+ }, {
+ Wrap(New("error with space"), "context"),
+ "%q",
+ `"context: error with space"`,
}}
for _, tt := range tests {
@@ -109,7 +113,7 @@
"EOF\n" +
"error\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:107",
+ "\t.+/github.com/pkg/errors/format_test.go:111",
}, {
Wrapf(New("error"), "error%d", 2),
"%v",
@@ -119,14 +123,14 @@
"%+v",
"error\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:118",
+ "\t.+/github.com/pkg/errors/format_test.go:122",
}, {
Wrap(Wrap(io.EOF, "error1"), "error2"),
"%+v",
"EOF\n" +
"error1\n" +
"github.com/pkg/errors.TestFormatWrapf\n" +
- "\t.+/github.com/pkg/errors/format_test.go:124\n",
+ "\t.+/github.com/pkg/errors/format_test.go:128\n",
}}
for _, tt := range tests {