@@ -107,4 +107,186 @@ func TestErrors(t *testing.T) {
107
107
assert .True (mt , netErr .Timeout (), "expected error %v to be a network timeout" , err )
108
108
})
109
109
})
110
+ mt .Run ("ServerError" , func (mt * mtest.T ) {
111
+ matchWrapped := errors .New ("wrapped err" )
112
+ otherWrapped := errors .New ("other err" )
113
+ const matchCode = 100
114
+ const otherCode = 120
115
+ const label = "testError"
116
+ testCases := []struct {
117
+ name string
118
+ err mongo.ServerError
119
+ hasCode bool
120
+ hasLabel bool
121
+ hasMessage bool
122
+ hasCodeWithMessage bool
123
+ isResult bool
124
+ }{
125
+ {
126
+ "CommandError all true" ,
127
+ mongo.CommandError {matchCode , "foo" , []string {label }, "name" , matchWrapped },
128
+ true ,
129
+ true ,
130
+ true ,
131
+ true ,
132
+ true ,
133
+ },
134
+ {
135
+ "CommandError all false" ,
136
+ mongo.CommandError {otherCode , "bar" , []string {"otherError" }, "name" , otherWrapped },
137
+ false ,
138
+ false ,
139
+ false ,
140
+ false ,
141
+ false ,
142
+ },
143
+ {
144
+ "CommandError has code not message" ,
145
+ mongo.CommandError {matchCode , "bar" , []string {}, "name" , nil },
146
+ true ,
147
+ false ,
148
+ false ,
149
+ false ,
150
+ false ,
151
+ },
152
+ {
153
+ "WriteException all in writeConcernError" ,
154
+ mongo.WriteException {
155
+ & mongo.WriteConcernError {"name" , matchCode , "foo" , nil },
156
+ nil ,
157
+ []string {label },
158
+ },
159
+ true ,
160
+ true ,
161
+ true ,
162
+ true ,
163
+ false ,
164
+ },
165
+ {
166
+ "WriteException all in writeError" ,
167
+ mongo.WriteException {
168
+ nil ,
169
+ mongo.WriteErrors {
170
+ mongo.WriteError {0 , otherCode , "bar" },
171
+ mongo.WriteError {0 , matchCode , "foo" },
172
+ },
173
+ []string {"otherError" },
174
+ },
175
+ true ,
176
+ false ,
177
+ true ,
178
+ true ,
179
+ false ,
180
+ },
181
+ {
182
+ "WriteException all false" ,
183
+ mongo.WriteException {
184
+ & mongo.WriteConcernError {"name" , otherCode , "bar" , nil },
185
+ mongo.WriteErrors {
186
+ mongo.WriteError {0 , otherCode , "baz" },
187
+ },
188
+ []string {"otherError" },
189
+ },
190
+ false ,
191
+ false ,
192
+ false ,
193
+ false ,
194
+ false ,
195
+ },
196
+ {
197
+ "WriteException HasErrorCodeAndMessage false" ,
198
+ mongo.WriteException {
199
+ & mongo.WriteConcernError {"name" , matchCode , "bar" , nil },
200
+ mongo.WriteErrors {
201
+ mongo.WriteError {0 , otherCode , "foo" },
202
+ },
203
+ []string {"otherError" },
204
+ },
205
+ true ,
206
+ false ,
207
+ true ,
208
+ false ,
209
+ false ,
210
+ },
211
+ {
212
+ "BulkWriteException all in writeConcernError" ,
213
+ mongo.BulkWriteException {
214
+ & mongo.WriteConcernError {"name" , matchCode , "foo" , nil },
215
+ nil ,
216
+ []string {label },
217
+ },
218
+ true ,
219
+ true ,
220
+ true ,
221
+ true ,
222
+ false ,
223
+ },
224
+ {
225
+ "BulkWriteException all in writeError" ,
226
+ mongo.BulkWriteException {
227
+ nil ,
228
+ []mongo.BulkWriteError {
229
+ {mongo.WriteError {0 , matchCode , "foo" }, & mongo.InsertOneModel {}},
230
+ {mongo.WriteError {0 , otherCode , "bar" }, & mongo.InsertOneModel {}},
231
+ },
232
+ []string {"otherError" },
233
+ },
234
+ true ,
235
+ false ,
236
+ true ,
237
+ true ,
238
+ false ,
239
+ },
240
+ {
241
+ "BulkWriteException all false" ,
242
+ mongo.BulkWriteException {
243
+ & mongo.WriteConcernError {"name" , otherCode , "bar" , nil },
244
+ []mongo.BulkWriteError {
245
+ {mongo.WriteError {0 , otherCode , "baz" }, & mongo.InsertOneModel {}},
246
+ },
247
+ []string {"otherError" },
248
+ },
249
+ false ,
250
+ false ,
251
+ false ,
252
+ false ,
253
+ false ,
254
+ },
255
+ {
256
+ "BulkWriteException HasErrorCodeAndMessage false" ,
257
+ mongo.BulkWriteException {
258
+ & mongo.WriteConcernError {"name" , matchCode , "bar" , nil },
259
+ []mongo.BulkWriteError {
260
+ {mongo.WriteError {0 , otherCode , "foo" }, & mongo.InsertOneModel {}},
261
+ },
262
+ []string {"otherError" },
263
+ },
264
+ true ,
265
+ false ,
266
+ true ,
267
+ false ,
268
+ false ,
269
+ },
270
+ }
271
+ for _ , tc := range testCases {
272
+ mt .Run (tc .name , func (mt * mtest.T ) {
273
+ has := tc .err .HasErrorCode (matchCode )
274
+ assert .Equal (mt , has , tc .hasCode , "expected HasErrorCode to return %v, got %v" , tc .hasCode , has )
275
+ has = tc .err .HasErrorLabel (label )
276
+ assert .Equal (mt , has , tc .hasLabel , "expected HasErrorLabel to return %v, got %v" , tc .hasLabel , has )
277
+
278
+ // Check for full message and substring
279
+ has = tc .err .HasErrorMessage ("foo" )
280
+ assert .Equal (mt , has , tc .hasMessage , "expected HasErrorMessage to return %v, got %v" , tc .hasMessage , has )
281
+ has = tc .err .HasErrorMessage ("fo" )
282
+ assert .Equal (mt , has , tc .hasMessage , "expected HasErrorMessage to return %v, got %v" , tc .hasMessage , has )
283
+ has = tc .err .HasErrorCodeWithMessage (matchCode , "foo" )
284
+ assert .Equal (mt , has , tc .hasCodeWithMessage , "expected HasErrorCodeWithMessage to return %v, got %v" , tc .hasCodeWithMessage , has )
285
+ has = tc .err .HasErrorCodeWithMessage (matchCode , "fo" )
286
+ assert .Equal (mt , has , tc .hasCodeWithMessage , "expected HasErrorCodeWithMessage to return %v, got %v" , tc .hasCodeWithMessage , has )
287
+
288
+ assert .Equal (mt , errors .Is (tc .err , matchWrapped ), tc .isResult , "expected errors.Is result to be %v" , tc .isResult )
289
+ })
290
+ }
291
+ })
110
292
}
0 commit comments