Skip to content

Commit 1ea066b

Browse files
author
Divjot Arora
committed
GODRIVER-1706 Ensure codeName is propagated for errors (#458)
1 parent a39a15a commit 1ea066b

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

mongo/errors.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,12 @@ func convertDriverWriteConcernError(wce *driver.WriteConcernError) *WriteConcern
244244
return nil
245245
}
246246

247-
return &WriteConcernError{Code: int(wce.Code), Message: wce.Message, Details: bson.Raw(wce.Details)}
247+
return &WriteConcernError{
248+
Name: wce.Name,
249+
Code: int(wce.Code),
250+
Message: wce.Message,
251+
Details: bson.Raw(wce.Details),
252+
}
248253
}
249254

250255
// BulkWriteError is an error that occurred during execution of one operation in a BulkWrite. This error type is only

mongo/integration/collection_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ const (
3232
errorModifiedID = 66
3333
)
3434

35-
func TestCollection(t *testing.T) {
36-
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
37-
defer mt.Close()
38-
35+
var (
3936
// impossibleWc is a write concern that can't be satisfied and is used to test write concern errors
4037
// for various operations. It includes a timeout because legacy servers will wait for all W nodes to respond,
4138
// causing tests to hang.
42-
impossibleWc := writeconcern.New(writeconcern.W(30), writeconcern.WTimeout(time.Second))
39+
impossibleWc = writeconcern.New(writeconcern.W(30), writeconcern.WTimeout(time.Second))
40+
)
41+
42+
func TestCollection(t *testing.T) {
43+
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
44+
defer mt.Close()
4345

4446
mt.RunOpts("insert one", noClientOpts, func(mt *mtest.T) {
4547
mt.Run("success", func(mt *mtest.T) {

mongo/integration/crud_prose_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,52 @@ func TestWriteConcernError(t *testing.T) {
232232
bson.Raw(errInfoDoc), wcError.Details)
233233
})
234234
}
235+
236+
func TestErrorsCodeNamePropagated(t *testing.T) {
237+
// Ensure the codeName field is propagated for both command and write concern errors.
238+
239+
mtOpts := mtest.NewOptions().
240+
Topologies(mtest.ReplicaSet).
241+
CreateClient(false)
242+
mt := mtest.New(t, mtOpts)
243+
defer mt.Close()
244+
245+
mt.RunOpts("command error", mtest.NewOptions().MinServerVersion("3.4"), func(mt *mtest.T) {
246+
// codeName is propagated in an ok:0 error.
247+
248+
cmd := bson.D{
249+
{"insert", mt.Coll.Name()},
250+
{"documents", []bson.D{}},
251+
}
252+
err := mt.DB.RunCommand(mtest.Background, cmd).Err()
253+
assert.NotNil(mt, err, "expected RunCommand error, got nil")
254+
255+
ce, ok := err.(mongo.CommandError)
256+
assert.True(mt, ok, "expected error of type %T, got %v of type %T", mongo.CommandError{}, err, err)
257+
expectedCodeName := "InvalidLength"
258+
assert.Equal(mt, expectedCodeName, ce.Name, "expected error code name %q, got %q", expectedCodeName, ce.Name)
259+
})
260+
261+
wcCollOpts := options.Collection().
262+
SetWriteConcern(impossibleWc)
263+
wcMtOpts := mtest.NewOptions().
264+
CollectionOptions(wcCollOpts)
265+
mt.RunOpts("write concern error", wcMtOpts, func(mt *mtest.T) {
266+
// codeName is propagated for write concern errors.
267+
268+
_, err := mt.Coll.InsertOne(mtest.Background, bson.D{})
269+
assert.NotNil(mt, err, "expected InsertOne error, got nil")
270+
271+
we, ok := err.(mongo.WriteException)
272+
assert.True(mt, ok, "expected error of type %T, got %v of type %T", mongo.WriteException{}, err, err)
273+
wce := we.WriteConcernError
274+
assert.NotNil(mt, wce, "expected write concern error, got %v", we)
275+
276+
var expectedCodeName string
277+
if codeNameVal, err := mt.GetSucceededEvent().Reply.LookupErr("writeConcernError", "codeName"); err == nil {
278+
expectedCodeName = codeNameVal.StringValue()
279+
}
280+
281+
assert.Equal(mt, expectedCodeName, wce.Name, "expected code name %q, got %q", expectedCodeName, wce.Name)
282+
})
283+
}

0 commit comments

Comments
 (0)