Skip to content

Commit 16d2050

Browse files
author
Divjot Arora
authored
GODRIVER-1536 Ensure errInfo is propagated (#344)
1 parent fcba78c commit 16d2050

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

mongo/integration/crud_prose_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.mongodb.org/mongo-driver/mongo/options"
1919
"go.mongodb.org/mongo-driver/mongo/readpref"
2020
"go.mongodb.org/mongo-driver/mongo/writeconcern"
21+
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2122
)
2223

2324
func TestWriteErrorsWithLabels(t *testing.T) {
@@ -225,3 +226,45 @@ func TestAggregateSecondaryPreferredReadPreference(t *testing.T) {
225226
assert.NotNil(mt, err, "expected command %s to not contain $readPreference", evt.Command)
226227
})
227228
}
229+
230+
func TestWriteConcernError(t *testing.T) {
231+
mt := mtest.New(t, noClientOpts)
232+
defer mt.Close()
233+
234+
errInfoOpts := mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
235+
mt.RunOpts("errInfo is propagated", errInfoOpts, func(mt *mtest.T) {
236+
wcDoc := bsoncore.BuildDocumentFromElements(nil,
237+
bsoncore.AppendInt32Element(nil, "w", 2),
238+
bsoncore.AppendInt32Element(nil, "wtimeout", 0),
239+
bsoncore.AppendStringElement(nil, "provenance", "clientSupplied"),
240+
)
241+
errInfoDoc := bsoncore.BuildDocumentFromElements(nil,
242+
bsoncore.AppendDocumentElement(nil, "writeConcern", wcDoc),
243+
)
244+
fp := mtest.FailPoint{
245+
ConfigureFailPoint: "failCommand",
246+
Mode: mtest.FailPointMode{
247+
Times: 1,
248+
},
249+
Data: mtest.FailPointData{
250+
FailCommands: []string{"insert"},
251+
WriteConcernError: &mtest.WriteConcernErrorData{
252+
Code: 100,
253+
Name: "UnsatisfiableWriteConcern",
254+
Errmsg: "Not enough data-bearing nodes",
255+
ErrInfo: errInfoDoc,
256+
},
257+
},
258+
}
259+
mt.SetFailPoint(fp)
260+
261+
_, err := mt.Coll.InsertOne(mtest.Background, bson.D{{"x", 1}})
262+
assert.NotNil(mt, err, "expected InsertOne error, got nil")
263+
writeException, ok := err.(mongo.WriteException)
264+
assert.True(mt, ok, "expected WriteException, got error %v of type %T", err, err)
265+
wcError := writeException.WriteConcernError
266+
assert.NotNil(mt, wcError, "expected write-concern error, got %v", err)
267+
assert.True(mt, bytes.Equal(wcError.Details, errInfoDoc), "expected errInfo document %v, got %v",
268+
bson.Raw(errInfoDoc), wcError.Details)
269+
})
270+
}

mongo/integration/mtest/mongotest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type WriteConcernErrorData struct {
7171
Name string `bson:"codeName"`
7272
Errmsg string `bson:"errmsg"`
7373
ErrorLabels *[]string `bson:"errorLabels,omitempty"`
74+
ErrInfo bson.Raw `bson:"errInfo,omitempty"`
7475
}
7576

7677
// T is a wrapper around testing.T.

0 commit comments

Comments
 (0)