IsDup function missing (golang)

Hello,
In order to check if an error is duplicate key error because index, I have to write mine (because I don’t find how to do this unlike with mgo driver). Is it possible to provide it in the Go driver?

Currently I use this:

// IsDup returns whether err informs of a duplicate key error because
// a primary key index or a secondary unique index already has an entry
// with the given value.
func IsDup(err error) bool {
	if wes, ok := err.(mongo.WriteException); ok {
		for i := range wes.WriteErrors {
			if wes.WriteErrors[i].Code == 11000 || wes.WriteErrors[i].Code == 11001 || wes.WriteErrors[i].Code == 12582 || wes.WriteErrors[i].Code == 16460 {
				return true
			}
		}
	}
	return false
}

Many thx

Hi @Jerome_LAFORGE,

There is an open ticket in our Jira project to add such a helper: https://jira.mongodb.org/browse/GODRIVER-972. It’s under the “Improve error types” project, which we’re hoping to get to in the coming quarter. For the time being, I think your IsDup helper looks good.

– Divjot

1 Like