[GODRIVER-972] add IsDuplicateKey(err) helper Created: 17/Apr/19  Updated: 02/Dec/20  Resolved: 02/Dec/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Sam Kleinman (Inactive) Assignee: Unassigned
Resolution: Duplicate Votes: 4
Labels: evg, mgocompat, neweng, rp-track
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates GODRIVER-1803 Add error API improvements helpers Closed
is duplicated by GODRIVER-707 method/operation to determine if a wr... Closed
Epic Link: GODRIVER-114

 Description   

This would be equivalent to MGO's "IsDup" and provide a simple way to handle duplicate key issues.

I think the implementation would look something like this:

func isMongoDupKey(err error) bool {
  wce, ok := err.(mongo.WriteConcernError)
  if !ok {
          return false
  }
  return wce.Code == 11000 || wce.Code == 11001 || wce.Code == 12582 || wce.Code == 16460 && strings.Contains(wce.Message, " E11000 ")
}



 Comments   
Comment by Ed Pelc [ 01/Dec/20 ]

Just FYI you need to check for WriteException(regular UpdateOne, UpdateMany, Insert*, etc), BulkWriteException(bulk variants), and CommandError(can be returned from FindOneAndUpdate).

Comment by Ian Whalen (Inactive) [ 18/Apr/19 ]

might want to make this a helper on the individual write error.

Generated at Thu Feb 08 08:35:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.