Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-standard, rich error types #325

Merged
merged 4 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func Time(key string, val time.Time) zapcore.Field {
// "error". If passed a nil error, the field is a no-op. This is purely a
// convenience for a common error-logging idiom; use String("someFieldName",
// err.Error()) to customize the key.
//
// Errors which also implement fmt.Formatter (like those produced by
// github.com/pkg/errors) will also have their verbose representation stored
// under "errorVerbose".
func Error(err error) zapcore.Field {
if err == nil {
return Skip()
Expand Down
18 changes: 10 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testImport:
- package: gopkg.in/inconshreveable/log15.v2
- package: github.com/mattn/goveralls
- package: github.com/pborman/uuid
- package: github.com/pkg/errors
- package: go.pedge.io/lion
- package: golang.org/x/tools
subpackages:
Expand Down
12 changes: 11 additions & 1 deletion zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ func (f Field) AddTo(enc ObjectEncoder) {
case StringerType:
enc.AddString(f.Key, f.Interface.(fmt.Stringer).String())
case ErrorType:
enc.AddString(f.Key, f.Interface.(error).Error())
val := f.Interface.(error)
basic := val.Error()
enc.AddString(f.Key, basic)
if fancy, ok := val.(fmt.Formatter); ok {
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)
}
}
case SkipType:
break
default:
Expand Down
35 changes: 33 additions & 2 deletions zapcore/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ package zapcore_test
import (
"errors"
"fmt"
"io"
"math"
"testing"
"time"

richErrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"

. "go.uber.org/zap/zapcore"
Expand All @@ -35,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 Expand Up @@ -125,5 +135,26 @@ func TestFields(t *testing.T) {
f := Field{Key: "k", Type: tt.t, Integer: tt.i, Interface: tt.iface, String: tt.s}
f.AddTo(enc)
assert.Equal(t, tt.want, enc.Fields["k"], "Unexpected output from field %+v.", f)

delete(enc.Fields, "k")
assert.Equal(t, 0, len(enc.Fields), "Unexpected extra fields present.")
}
}

func TestRichErrorSupport(t *testing.T) {
f := Field{
Type: ErrorType,
Interface: richErrors.WithMessage(richErrors.New("egad"), "failed"),
Key: "k",
}
enc := NewMapObjectEncoder()
f.AddTo(enc)
assert.Equal(t, "failed: egad", enc.Fields["k"], "Unexpected basic error message.")

serialized := enc.Fields["kVerbose"]
// Don't assert the exact format used by a third-party package, but ensure
// that some critical elements are present.
assert.Regexp(t, `egad`, serialized, "Expected original error message to be present.")
assert.Regexp(t, `failed`, serialized, "Expected error annotation to be present.")
assert.Regexp(t, `TestRichErrorSupport`, serialized, "Expected calling function to be present in stacktrace.")
}