Skip to content

Commit a81b924

Browse files
committed
Test error marshal failed
1 parent 568f031 commit a81b924

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

wrap_error_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ func TestWrapErrorDuplicateField(t *testing.T) {
9696
},
9797
}, enc.Fields)
9898
}
99+
100+
func TestWrapErrorMarshalErr(t *testing.T) {
101+
102+
}

zapcore/error_test.go

+38-10
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,24 @@ func (e customMultierr) Errors() []error {
6868
}
6969
}
7070

71+
type customErrObject struct{}
72+
73+
func (customErrObject) Error() string { return "custom err" }
74+
func (customErrObject) MarshalLogObject(enc ObjectEncoder) error {
75+
enc.AddInt("count", 1)
76+
return errors.New("marshal failed")
77+
}
78+
7179
func TestErrorEncoding(t *testing.T) {
7280
tests := []struct {
81+
msg string
7382
k string
7483
t FieldType // defaults to ErrorType
7584
iface interface{}
7685
want map[string]interface{}
7786
}{
7887
{
88+
msg: "custom key and fields",
7989
k: "k",
8090
iface: errTooManyUsers(2),
8191
want: map[string]interface{}{
@@ -86,7 +96,8 @@ func TestErrorEncoding(t *testing.T) {
8696
},
8797
},
8898
{
89-
k: "err",
99+
msg: "multierr",
100+
k: "err",
90101
iface: multierr.Combine(
91102
errors.New("foo"),
92103
errors.New("bar"),
@@ -102,6 +113,7 @@ func TestErrorEncoding(t *testing.T) {
102113
},
103114
},
104115
{
116+
msg: "nested error causes",
105117
k: "e",
106118
iface: customMultierr{},
107119
want: map[string]interface{}{
@@ -119,14 +131,16 @@ func TestErrorEncoding(t *testing.T) {
119131
},
120132
},
121133
{
134+
msg: "simple error",
122135
k: "k",
123136
iface: fmt.Errorf("failed: %w", errors.New("egad")),
124137
want: map[string]interface{}{
125138
"k": "failed: egad",
126139
},
127140
},
128141
{
129-
k: "error",
142+
msg: "multierr with causes",
143+
k: "error",
130144
iface: multierr.Combine(
131145
fmt.Errorf("hello: %w",
132146
multierr.Combine(errors.New("foo"), errors.New("bar")),
@@ -145,17 +159,31 @@ func TestErrorEncoding(t *testing.T) {
145159
},
146160
},
147161
},
162+
{
163+
msg: "error fields marshal failed",
164+
k: "error",
165+
iface: customErrObject{},
166+
want: map[string]interface{}{
167+
"error": "custom err",
168+
"errorError": "marshal failed",
169+
"errorFields": map[string]interface{}{
170+
"count": 1,
171+
},
172+
},
173+
},
148174
}
149175

150176
for _, tt := range tests {
151-
if tt.t == UnknownType {
152-
tt.t = ErrorType
153-
}
154-
155-
enc := NewMapObjectEncoder()
156-
f := Field{Key: tt.k, Type: tt.t, Interface: tt.iface}
157-
f.AddTo(enc)
158-
assert.Equal(t, tt.want, enc.Fields, "Unexpected output from field %+v.", f)
177+
t.Run(tt.msg, func(t *testing.T) {
178+
if tt.t == UnknownType {
179+
tt.t = ErrorType
180+
}
181+
182+
enc := NewMapObjectEncoder()
183+
f := Field{Key: tt.k, Type: tt.t, Interface: tt.iface}
184+
f.AddTo(enc)
185+
assert.Equal(t, tt.want, enc.Fields, "Unexpected output from field %+v.", f)
186+
})
159187
}
160188
}
161189

0 commit comments

Comments
 (0)