Skip to content

Commit c8764eb

Browse files
author
Divjot Arora
committed
Wrap command.QueryFailureError in mongo.CommandError
GODRIVER-1141 Change-Id: Iae949cb9ced7bdaa090f84365e34a4547c037ccd
1 parent 0f4dd65 commit c8764eb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

mongo/collection_internal_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,20 @@ func TestCollection_Find_Error(t *testing.T) {
17451745
require.NotNil(t, c.Err())
17461746
_ = c.Close(context.Background())
17471747
})
1748+
1749+
t.Run("error type", func(t *testing.T) {
1750+
coll := createTestCollection(t, nil, nil)
1751+
initCollection(t, coll)
1752+
c, err := coll.Find(ctx, bson.D{}, options.Find().SetHint("foobar"))
1753+
if err == nil {
1754+
_ = c.Close(ctx)
1755+
t.Fatal("expected bad hint error but got nil")
1756+
}
1757+
_, ok := err.(CommandError)
1758+
if !ok {
1759+
t.Fatalf("err type mismatch; expected CommandError, got %T", err)
1760+
}
1761+
})
17481762
}
17491763

17501764
func TestCollection_Find_NegativeLimit(t *testing.T) {

mongo/errors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ func replaceErrors(err error) error {
4747
WriteErrors: convertBulkWriteErrors(conv.WriteErrors),
4848
}
4949
}
50+
if qe, ok := err.(command.QueryFailureError); ok {
51+
// qe.Message is "command failure"
52+
ce := CommandError{Name: qe.Message}
53+
54+
dollarErr, err := qe.Response.LookupErr("$err")
55+
if err == nil {
56+
ce.Message, _ = dollarErr.StringValueOK()
57+
}
58+
code, err := qe.Response.LookupErr("code")
59+
if err == nil {
60+
ce.Code, _ = code.Int32OK()
61+
}
62+
63+
return ce
64+
}
5065

5166
return err
5267
}

0 commit comments

Comments
 (0)