Skip to content

Commit 023c3eb

Browse files
committed
Return results from runCommand in error cases
GODRIVER-832 Change-Id: I5dc892d7cab5e3d8cf03dc3acda3bc77787a0a90
1 parent 9ec4480 commit 023c3eb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

mongo/single_result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func (sr *SingleResult) Decode(v interface{}) error {
4848

4949
// DecodeBytes will return a copy of the document as a bson.Raw. If there was an
5050
// error from the operation that created this SingleResult then the error
51-
// will be returned. If there were no returned documents, ErrNoDocuments is
51+
// will be returned along with the result. If there were no returned documents, ErrNoDocuments is
5252
// returned.
5353
func (sr *SingleResult) DecodeBytes() (bson.Raw, error) {
5454
if sr.err != nil {
55-
return nil, sr.err
55+
return sr.rdr, sr.err
5656
}
5757

5858
if sr.err = sr.setRdrContents(); sr.err != nil {

mongo/single_result_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package mongo
88

99
import (
1010
"bytes"
11+
"errors"
1112
"testing"
1213

1314
"go.mongodb.org/mongo-driver/bson"
@@ -53,4 +54,16 @@ func TestSingleResult(t *testing.T) {
5354
t.Fatalf("Error returned by SingleResult.Err() was %v when ErrNoDocuments was expected", err)
5455
}
5556
})
57+
58+
t.Run("TestDecodeWithErr", func(t *testing.T) {
59+
r := []byte("foo")
60+
sr := &SingleResult{rdr: r, err: errors.New("DecodeBytes error")}
61+
res, err := sr.DecodeBytes()
62+
if !bytes.Equal(res, r) {
63+
t.Fatalf("DecodeBytes contents do not match; expected %v, returned %v", res, r)
64+
}
65+
if err != sr.err {
66+
t.Fatalf("Error returned by DecodeBytes was %v when %v was expected", err, sr.err)
67+
}
68+
})
5669
}

0 commit comments

Comments
 (0)