Skip to content

Commit c255636

Browse files
emilien-pugetDivjot Arora
authored andcommitted
GODRIVER-1450 : use correct writeModel in BulkWriteError (#263)
1 parent ad060a4 commit c255636

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

mongo/bulk_write.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (bw *bulkWrite) runBatch(ctx context.Context, batch bulkWriteBatch) (BulkWr
153153
for _, we := range convWriteErrors {
154154
batchErr.WriteErrors = append(batchErr.WriteErrors, BulkWriteError{
155155
WriteError: we,
156-
Request: batch.models[0],
156+
Request: batch.models[we.Index],
157157
})
158158
}
159159
return batchRes, batchErr, nil

mongo/integration/collection_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,35 @@ func TestCollection(t *testing.T) {
964964
})
965965
}
966966
})
967+
mt.Run("correct model in errors", func(mt *mtest.T) {
968+
models := []mongo.WriteModel{
969+
mongo.NewUpdateOneModel().SetFilter(bson.M{}).SetUpdate(bson.M{}),
970+
mongo.NewInsertOneModel().SetDocument(bson.M{
971+
"_id": "notduplicate",
972+
}),
973+
mongo.NewInsertOneModel().SetDocument(bson.M{
974+
"_id": "duplicate1",
975+
}),
976+
mongo.NewInsertOneModel().SetDocument(bson.M{
977+
"_id": "duplicate1",
978+
}),
979+
mongo.NewInsertOneModel().SetDocument(bson.M{
980+
"_id": "duplicate2",
981+
}),
982+
mongo.NewInsertOneModel().SetDocument(bson.M{
983+
"_id": "duplicate2",
984+
}),
985+
}
986+
987+
_, err := mt.Coll.BulkWrite(mtest.Background, models)
988+
bwException, ok := err.(mongo.BulkWriteException)
989+
assert.True(mt, ok, "expected error of type %T, got %T", mongo.BulkWriteException{}, err)
990+
991+
expectedModel := models[3]
992+
actualModel := bwException.WriteErrors[0].Request
993+
assert.Equal(mt, expectedModel, actualModel, "expected model %v in BulkWriteException, got %v",
994+
expectedModel, actualModel)
995+
})
967996
})
968997
}
969998

0 commit comments

Comments
 (0)