Skip to content

Commit

Permalink
Support double-wrapping of error + fields
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv committed Apr 12, 2023
1 parent a99527f commit 29186ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fields_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (e errorWithFields) Error() string {
}

func (e errorWithFields) MarshalLogObject(oe zapcore.ObjectEncoder) error {
if nestedMarshaler, ok := e.err.(zapcore.ObjectMarshaler); ok {
nestedMarshaler.MarshalLogObject(oe)
}
for _, f := range e.fields {
f.AddTo(oe)
}
Expand Down
23 changes: 23 additions & 0 deletions fields_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,27 @@ func TestErrorWithFields(t *testing.T) {
"count": int64(12),
},
}, enc.Fields)

wrapped2 := WrapError(wrapped, Bool("wrap2", true))
Error(wrapped2).AddTo(enc)
assert.Equal(t, map[string]any{
"error": rootErr.Error(),
"errorFields": map[string]any{
"user": "foo",
"count": int64(12),
"wrap2": true,
},
}, enc.Fields)

wrapped3 := WrapError(wrapped2, Bool("wrap3", true))
Error(wrapped3).AddTo(enc)
assert.Equal(t, map[string]any{
"error": rootErr.Error(),
"errorFields": map[string]any{
"user": "foo",
"count": int64(12),
"wrap2": true,
"wrap3": true,
},
}, enc.Fields)
}

0 comments on commit 29186ef

Please sign in to comment.