Skip to content

Commit

Permalink
Only include verbose error if different
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Feb 20, 2017
1 parent 2059b8a commit 4105466
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 8 additions & 4 deletions zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ func (f Field) AddTo(enc ObjectEncoder) {
enc.AddString(f.Key, f.Interface.(fmt.Stringer).String())
case ErrorType:
val := f.Interface.(error)
basic := val.Error()
enc.AddString(f.Key, basic)
if fancy, ok := val.(fmt.Formatter); ok {
// This is a rich error type, like those produced by
// github.com/pkg/errors.
enc.AddString(f.Key+"Verbose", fmt.Sprintf("%+v", fancy))
verbose := fmt.Sprintf("%+v", fancy)
if verbose != basic {
// This is a rich error type, like those produced by
// github.com/pkg/errors.
enc.AddString(f.Key+"Verbose", verbose)
}
}
enc.AddString(f.Key, val.Error())
case SkipType:
break
default:
Expand Down
13 changes: 11 additions & 2 deletions zapcore/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package zapcore_test
import (
"errors"
"fmt"
"io"
"math"
"testing"
"time"
Expand All @@ -36,11 +37,19 @@ import (
type users int

func (u users) String() string {
return fmt.Sprintf("%d users", u)
return fmt.Sprintf("%d users", int(u))
}

func (u users) Error() string {
return fmt.Sprintf("%d too many users", u)
return fmt.Sprintf("%d too many users", int(u))
}

func (u users) Format(s fmt.State, verb rune) {
// Implement fmt.Formatter, but don't add any information beyond the basic
// Error method.
if verb == 'v' && s.Flag('+') {
io.WriteString(s, u.Error())
}
}

func (u users) MarshalLogObject(enc ObjectEncoder) error {
Expand Down

0 comments on commit 4105466

Please sign in to comment.