Skip to content

Commit 7b6cb77

Browse files
committed
Improve UnknownFieldError message
Signed-off-by: Tim Barry <tim.barry.business+github@gmail.com>
1 parent 0a60138 commit 7b6cb77

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

decode.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ func (e *DupMapKeyError) Error() string {
207207

208208
// UnknownFieldError describes detected unknown field in CBOR map when decoding to Go struct.
209209
type UnknownFieldError struct {
210-
Index int
210+
Struct string // name of the struct being decoded
211+
Field string // field of the CBOR map that was unexpected
212+
Index int // index of the field in the CBOR map
211213
}
212214

213215
func (e *UnknownFieldError) Error() string {
214-
return fmt.Sprintf("cbor: found unknown field at map element index %d", e.Index)
216+
return fmt.Sprintf("cbor: found unknown field: struct '%s' has no field '%s', at map element index %d", e.Struct, e.Field, e.Index)
215217
}
216218

217219
// UnacceptableDataItemError is returned when unmarshaling a CBOR input that contains a data item
@@ -2617,8 +2619,8 @@ MapEntryLoop:
26172619
var k any
26182620

26192621
t := d.nextCBORType()
2622+
var keyBytes []byte
26202623
if t == cborTypeTextString || (t == cborTypeByteString && d.dm.fieldNameByteString == FieldNameByteStringAllowed) {
2621-
var keyBytes []byte
26222624
if t == cborTypeTextString {
26232625
keyBytes, lastErr = d.parseTextString()
26242626
if lastErr != nil {
@@ -2767,7 +2769,7 @@ MapEntryLoop:
27672769

27682770
if f == nil {
27692771
if errOnUnknownField {
2770-
err = &UnknownFieldError{j}
2772+
err = &UnknownFieldError{Struct: tInfo.typ.String(), Field: string(keyBytes), Index: j}
27712773
d.skip() // Skip value
27722774
j++
27732775
// skip the rest of the map

decode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6825,7 +6825,7 @@ func TestExtraErrorCondUnknownField(t *testing.T) {
68256825
name: "CBOR map unknown field with ExtraDecErrorUnknownField",
68266826
data: hexDecode("a461416161614261626143616361446164"), // map[string]string{"A": "a", "B": "b", "C": "c", "D": "d"}
68276827
dm: dmUnknownFieldError,
6828-
wantErrorMsg: "cbor: found unknown field at map element index 3",
6828+
wantErrorMsg: "cbor: found unknown field: struct 'cbor.s' has no field 'D', at map element index 3",
68296829
},
68306830
}
68316831
for _, tc := range testCases {
@@ -6885,7 +6885,7 @@ func TestStreamExtraErrorCondUnknownField(t *testing.T) {
68856885
}
68866886

68876887
data := hexDecode("a461416161614461646142616261436163a3614161616142616261436163") // map[string]string{"A": "a", "D": "d", "B": "b", "C": "c"}, map[string]string{"A": "a", "B": "b", "C": "c"}
6888-
wantErrorMsg := "cbor: found unknown field at map element index 1"
6888+
wantErrorMsg := "cbor: found unknown field: struct 'cbor.s' has no field 'D', at map element index 1"
68896889
wantObj := s{A: "a", B: "b", C: "c"}
68906890

68916891
dmUnknownFieldError, _ := DecOptions{ExtraReturnErrors: ExtraDecErrorUnknownField}.DecMode()

0 commit comments

Comments
 (0)