@@ -252,9 +252,25 @@ type ServerError interface {
252
252
// HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.
253
253
HasErrorCodeWithMessage (int , string ) bool
254
254
255
+ // ErrorCodes returns all error codes (unsorted) in the server’s response.
256
+ // This would include nested errors (e.g., write concern errors) for
257
+ // supporting implementations (e.g., BulkWriteException) as well as the
258
+ // top-level error code.
259
+ ErrorCodes () []int
260
+
255
261
serverError ()
256
262
}
257
263
264
+ func hasErrorCode (srvErr ServerError , code int ) bool {
265
+ for _ , srvErrCode := range srvErr .ErrorCodes () {
266
+ if code == srvErrCode {
267
+ return true
268
+ }
269
+ }
270
+
271
+ return false
272
+ }
273
+
258
274
var _ ServerError = CommandError {}
259
275
var _ ServerError = WriteError {}
260
276
var _ ServerError = WriteException {}
@@ -290,6 +306,11 @@ func (e CommandError) HasErrorCode(code int) bool {
290
306
return int (e .Code ) == code
291
307
}
292
308
309
+ // ErrorCodes returns a list of error codes returned by the server.
310
+ func (e CommandError ) ErrorCodes () []int {
311
+ return []int {int (e .Code )}
312
+ }
313
+
293
314
// HasErrorLabel returns true if the error contains the specified label.
294
315
func (e CommandError ) HasErrorLabel (label string ) bool {
295
316
for _ , l := range e .Labels {
@@ -345,6 +366,11 @@ func (we WriteError) HasErrorCode(code int) bool {
345
366
return we .Code == code
346
367
}
347
368
369
+ // ErrorCodes returns a list of error codes returned by the server.
370
+ func (we WriteError ) ErrorCodes () []int {
371
+ return []int {we .Code }
372
+ }
373
+
348
374
// HasErrorLabel returns true if the error contains the specified label. WriteErrors do not contain labels,
349
375
// so we always return false.
350
376
func (we WriteError ) HasErrorLabel (string ) bool {
@@ -451,15 +477,21 @@ func (mwe WriteException) Error() string {
451
477
452
478
// HasErrorCode returns true if the error has the specified code.
453
479
func (mwe WriteException ) HasErrorCode (code int ) bool {
454
- if mwe .WriteConcernError != nil && mwe .WriteConcernError .Code == code {
455
- return true
480
+ return hasErrorCode (mwe , code )
481
+ }
482
+
483
+ // ErrorCodes returns a list of error codes returned by the server.
484
+ func (mwe WriteException ) ErrorCodes () []int {
485
+ errorCodes := []int {}
486
+ for _ , writeError := range mwe .WriteErrors {
487
+ errorCodes = append (errorCodes , writeError .Code )
456
488
}
457
- for _ , we := range mwe .WriteErrors {
458
- if we .Code == code {
459
- return true
460
- }
489
+
490
+ if mwe .WriteConcernError != nil {
491
+ errorCodes = append (errorCodes , mwe .WriteConcernError .Code )
461
492
}
462
- return false
493
+
494
+ return errorCodes
463
495
}
464
496
465
497
// HasErrorLabel returns true if the error contains the specified label.
@@ -563,15 +595,21 @@ func (bwe BulkWriteException) Error() string {
563
595
564
596
// HasErrorCode returns true if any of the errors have the specified code.
565
597
func (bwe BulkWriteException ) HasErrorCode (code int ) bool {
566
- if bwe .WriteConcernError != nil && bwe .WriteConcernError .Code == code {
567
- return true
598
+ return hasErrorCode (bwe , code )
599
+ }
600
+
601
+ // ErrorCodes returns a list of error codes returned by the server.
602
+ func (bwe BulkWriteException ) ErrorCodes () []int {
603
+ errorCodes := []int {}
604
+ for _ , writeError := range bwe .WriteErrors {
605
+ errorCodes = append (errorCodes , writeError .Code )
568
606
}
569
- for _ , we := range bwe .WriteErrors {
570
- if we .Code == code {
571
- return true
572
- }
607
+
608
+ if bwe .WriteConcernError != nil {
609
+ errorCodes = append (errorCodes , bwe .WriteConcernError .Code )
573
610
}
574
- return false
611
+
612
+ return errorCodes
575
613
}
576
614
577
615
// HasErrorLabel returns true if the error contains the specified label.
0 commit comments