Skip to content

Commit d3f6dae

Browse files
reillywatsonBenjamin Rewis
authored andcommitted
GODRIVER-2243 Wrap write errors in IndexView.CreateMany (#820)
1 parent 3c1c1d7 commit d3f6dae

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mongo/index_view.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ..
272272

273273
err = op.Execute(ctx)
274274
if err != nil {
275-
return nil, replaceErrors(err)
275+
_, err = processWriteError(err)
276+
return nil, err
276277
}
277278

278279
return names, nil

mongo/integration/index_view_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ func TestIndexView(t *testing.T) {
263263
})
264264
}
265265
})
266+
unackClientOpts := options.Client().
267+
SetWriteConcern(writeconcern.New(writeconcern.W(0)))
268+
unackMtOpts := mtest.NewOptions().
269+
ClientOptions(unackClientOpts).
270+
MinServerVersion("3.6")
271+
mt.RunOpts("unacknowledged write", unackMtOpts, func(mt *mtest.T) {
272+
_, err := mt.Coll.Indexes().CreateOne(mtest.Background, mongo.IndexModel{Keys: bson.D{{"x", 1}}})
273+
if err != mongo.ErrUnacknowledgedWrite {
274+
// Use a direct comparison rather than assert.Equal because assert.Equal will compare the error strings,
275+
// so the assertion would succeed even if the error had not been wrapped.
276+
mt.Fatalf("expected CreateOne error %v, got %v", mongo.ErrUnacknowledgedWrite, err)
277+
}
278+
})
266279
// Needs to run on these versions for failpoints
267280
mt.RunOpts("replace error", mtest.NewOptions().Topologies(mtest.ReplicaSet).MinServerVersion("4.0"), func(mt *mtest.T) {
268281
mt.SetFailPoint(mtest.FailPoint{

0 commit comments

Comments
 (0)