@@ -252,9 +252,25 @@ type ServerError interface {
252252 // HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.
253253 HasErrorCodeWithMessage (int , string ) bool
254254
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+
255261 serverError ()
256262}
257263
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+
258274var _ ServerError = CommandError {}
259275var _ ServerError = WriteError {}
260276var _ ServerError = WriteException {}
@@ -290,6 +306,11 @@ func (e CommandError) HasErrorCode(code int) bool {
290306 return int (e .Code ) == code
291307}
292308
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+
293314// HasErrorLabel returns true if the error contains the specified label.
294315func (e CommandError ) HasErrorLabel (label string ) bool {
295316 for _ , l := range e .Labels {
@@ -345,6 +366,11 @@ func (we WriteError) HasErrorCode(code int) bool {
345366 return we .Code == code
346367}
347368
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+
348374// HasErrorLabel returns true if the error contains the specified label. WriteErrors do not contain labels,
349375// so we always return false.
350376func (we WriteError ) HasErrorLabel (string ) bool {
@@ -451,15 +477,21 @@ func (mwe WriteException) Error() string {
451477
452478// HasErrorCode returns true if the error has the specified code.
453479func (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 )
456488 }
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 )
461492 }
462- return false
493+
494+ return errorCodes
463495}
464496
465497// HasErrorLabel returns true if the error contains the specified label.
@@ -563,15 +595,21 @@ func (bwe BulkWriteException) Error() string {
563595
564596// HasErrorCode returns true if any of the errors have the specified code.
565597func (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 )
568606 }
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 )
573610 }
574- return false
611+
612+ return errorCodes
575613}
576614
577615// HasErrorLabel returns true if the error contains the specified label.
0 commit comments