Skip to content

Commit

Permalink
optimize for errors which don't implement fmt.Formatter
Browse files Browse the repository at this point in the history
If the error doesn't implement fmt.Formatter, as most won't, its faster to use err.Error() than always use fmt to print the error.
  • Loading branch information
ansel1 committed Jan 10, 2025
1 parent 3a2a416 commit 7cda51f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ func (e encoder) writeValue(buf *buffer, value slog.Value) {
case slog.KindAny:
switch v := value.Any().(type) {
case error:
e.withColor(buf, e.opts.Theme.AttrValueError(), func() {
fmt.Fprintf(buf, "%+v", v)
})
if _, ok := v.(fmt.Formatter); ok {
e.withColor(buf, e.opts.Theme.AttrValueError(), func() {
fmt.Fprintf(buf, "%+v", v)
})
} else {
e.writeColoredString(buf, v.Error(), e.opts.Theme.AttrValueError())
}
return
case fmt.Stringer:
e.writeColoredString(buf, v.String(), attrValue)
Expand Down

0 comments on commit 7cda51f

Please sign in to comment.