@@ -68,14 +68,24 @@ func (e customMultierr) Errors() []error {
68
68
}
69
69
}
70
70
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
+
71
79
func TestErrorEncoding (t * testing.T ) {
72
80
tests := []struct {
81
+ msg string
73
82
k string
74
83
t FieldType // defaults to ErrorType
75
84
iface interface {}
76
85
want map [string ]interface {}
77
86
}{
78
87
{
88
+ msg : "custom key and fields" ,
79
89
k : "k" ,
80
90
iface : errTooManyUsers (2 ),
81
91
want : map [string ]interface {}{
@@ -86,7 +96,8 @@ func TestErrorEncoding(t *testing.T) {
86
96
},
87
97
},
88
98
{
89
- k : "err" ,
99
+ msg : "multierr" ,
100
+ k : "err" ,
90
101
iface : multierr .Combine (
91
102
errors .New ("foo" ),
92
103
errors .New ("bar" ),
@@ -102,6 +113,7 @@ func TestErrorEncoding(t *testing.T) {
102
113
},
103
114
},
104
115
{
116
+ msg : "nested error causes" ,
105
117
k : "e" ,
106
118
iface : customMultierr {},
107
119
want : map [string ]interface {}{
@@ -119,14 +131,16 @@ func TestErrorEncoding(t *testing.T) {
119
131
},
120
132
},
121
133
{
134
+ msg : "simple error" ,
122
135
k : "k" ,
123
136
iface : fmt .Errorf ("failed: %w" , errors .New ("egad" )),
124
137
want : map [string ]interface {}{
125
138
"k" : "failed: egad" ,
126
139
},
127
140
},
128
141
{
129
- k : "error" ,
142
+ msg : "multierr with causes" ,
143
+ k : "error" ,
130
144
iface : multierr .Combine (
131
145
fmt .Errorf ("hello: %w" ,
132
146
multierr .Combine (errors .New ("foo" ), errors .New ("bar" )),
@@ -145,17 +159,31 @@ func TestErrorEncoding(t *testing.T) {
145
159
},
146
160
},
147
161
},
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
+ },
148
174
}
149
175
150
176
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
+ })
159
187
}
160
188
}
161
189
0 commit comments