Skip to content

Commit 1ada5fa

Browse files
authored
GODRIVER-3425 Reference ObjectIdAsHexString decoder option in exception message. (#1896)
1 parent 17aeb0b commit 1ada5fa

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

bson/decoder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ func TestDecoderConfiguration(t *testing.T) {
647647

648648
var got objectIDTest
649649
err := dec.Decode(&got)
650-
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
650+
const want = "error decoding key id: decoding an object ID into a string is not supported by default (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
651+
assert.EqualError(t, err, want)
651652
})
652653
t.Run("DefaultDocumentM top-level", func(t *testing.T) {
653654
t.Parallel()

bson/string_codec.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package bson
88

99
import (
10+
"errors"
1011
"fmt"
1112
"reflect"
1213
)
@@ -50,14 +51,16 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty
5051
return emptyValue, err
5152
}
5253
case TypeObjectID:
53-
oid, err := vr.ReadObjectID()
54-
if err != nil {
55-
return emptyValue, err
56-
}
5754
if dc.objectIDAsHexString {
55+
oid, err := vr.ReadObjectID()
56+
if err != nil {
57+
return emptyValue, err
58+
}
5859
str = oid.Hex()
5960
} else {
60-
return emptyValue, fmt.Errorf("decoding an object ID to a non-hexadecimal string representation is not supported")
61+
const msg = "decoding an object ID into a string is not supported by default " +
62+
"(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
63+
return emptyValue, errors.New(msg)
6164
}
6265
case TypeSymbol:
6366
str, err = vr.ReadSymbol()

0 commit comments

Comments
 (0)