@@ -18,6 +18,7 @@ import (
18
18
"go.mongodb.org/mongo-driver/mongo/options"
19
19
"go.mongodb.org/mongo-driver/mongo/readpref"
20
20
"go.mongodb.org/mongo-driver/mongo/writeconcern"
21
+ "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
21
22
)
22
23
23
24
func TestWriteErrorsWithLabels (t * testing.T ) {
@@ -225,3 +226,45 @@ func TestAggregateSecondaryPreferredReadPreference(t *testing.T) {
225
226
assert .NotNil (mt , err , "expected command %s to not contain $readPreference" , evt .Command )
226
227
})
227
228
}
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
+ }
0 commit comments