Skip to content

Commit d130800

Browse files
author
Divjot Arora
committed
GODRIVER-1562 Wrap driver.ErrUnacknowledgedWrite in BulkWrite (#361)
1 parent 024606b commit d130800

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mongo/bulk_write.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func (bw *bulkWrite) execute(ctx context.Context) error {
7575

7676
bwErr.WriteErrors = append(bwErr.WriteErrors, batchErr.WriteErrors...)
7777

78-
if !continueOnError && (err != nil || len(batchErr.WriteErrors) > 0 || batchErr.WriteConcernError != nil) {
78+
commandErrorOccurred := err != nil && err != driver.ErrUnacknowledgedWrite
79+
writeErrorOccurred := len(batchErr.WriteErrors) > 0 || batchErr.WriteConcernError != nil
80+
if !continueOnError && (commandErrorOccurred || writeErrorOccurred) {
7981
if err != nil {
8082
return err
8183
}
@@ -92,6 +94,7 @@ func (bw *bulkWrite) execute(ctx context.Context) error {
9294

9395
bw.result.MatchedCount -= bw.result.UpsertedCount
9496
if lastErr != nil {
97+
_, lastErr = processWriteError(lastErr)
9598
return lastErr
9699
}
97100
if len(bwErr.WriteErrors) > 0 || bwErr.WriteConcernError != nil {

mongo/integration/collection_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,22 @@ func TestCollection(t *testing.T) {
993993
assert.Equal(mt, expectedModel, actualModel, "expected model %v in BulkWriteException, got %v",
994994
expectedModel, actualModel)
995995
})
996+
unackClientOpts := options.Client().
997+
SetWriteConcern(writeconcern.New(writeconcern.W(0)))
998+
unackMtOpts := mtest.NewOptions().
999+
ClientOptions(unackClientOpts).
1000+
MinServerVersion("3.6")
1001+
mt.RunOpts("unacknowledged write", unackMtOpts, func(mt *mtest.T) {
1002+
models := []mongo.WriteModel{
1003+
mongo.NewInsertOneModel().SetDocument(bson.D{{"x", 1}}),
1004+
}
1005+
_, err := mt.Coll.BulkWrite(mtest.Background, models)
1006+
if err != mongo.ErrUnacknowledgedWrite {
1007+
// Use a direct comparison rather than assert.Equal because assert.Equal will compare the error strings,
1008+
// so the assertion would succeed even if the error had not been wrapped.
1009+
mt.Fatalf("expected BulkWrite error %v, got %v", mongo.ErrUnacknowledgedWrite, err)
1010+
}
1011+
})
9961012
})
9971013
}
9981014

0 commit comments

Comments
 (0)