Skip to content

Commit c1361f0

Browse files
authored
Merge pull request #119 from oschwald/greg/improve-errors
Improve error messages when unmarshaling map values
2 parents 64f043c + 32f205a commit c1361f0

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

decoder.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package maxminddb
22

33
import (
44
"encoding/binary"
5+
"fmt"
56
"math"
67
"math/big"
78
"reflect"
@@ -418,7 +419,7 @@ func (d *decoder) unmarshalMap(
418419
result = indirect(result)
419420
switch result.Kind() {
420421
default:
421-
return 0, newUnmarshalTypeError("map", result.Type())
422+
return 0, newUnmarshalTypeStrError("map", result.Type())
422423
case reflect.Struct:
423424
return d.decodeStruct(size, offset, result, depth)
424425
case reflect.Map:
@@ -430,7 +431,7 @@ func (d *decoder) unmarshalMap(
430431
result.Set(rv)
431432
return newOffset, err
432433
}
433-
return 0, newUnmarshalTypeError("map", result.Type())
434+
return 0, newUnmarshalTypeStrError("map", result.Type())
434435
}
435436
}
436437

@@ -465,7 +466,7 @@ func (d *decoder) unmarshalSlice(
465466
return newOffset, err
466467
}
467468
}
468-
return 0, newUnmarshalTypeError("array", result.Type())
469+
return 0, newUnmarshalTypeStrError("array", result.Type())
469470
}
470471

471472
func (d *decoder) unmarshalString(size, offset uint, result reflect.Value) (uint, error) {
@@ -615,7 +616,7 @@ func (d *decoder) decodeMap(
615616

616617
offset, err = d.decode(offset, elemValue, depth)
617618
if err != nil {
618-
return 0, err
619+
return 0, fmt.Errorf("decoding value for %s: %w", key, err)
619620
}
620621

621622
keyValue.SetString(string(key))
@@ -772,7 +773,7 @@ func (d *decoder) decodeStruct(
772773

773774
offset, err = d.decode(offset, result.Field(j), depth)
774775
if err != nil {
775-
return 0, err
776+
return 0, fmt.Errorf("decoding value for %s: %w", key, err)
776777
}
777778
}
778779
return offset, nil

errors.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ type UnmarshalTypeError struct {
3030
Value string
3131
}
3232

33-
func newUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError {
33+
func newUnmarshalTypeStrError(value string, rType reflect.Type) UnmarshalTypeError {
3434
return UnmarshalTypeError{
35-
Value: fmt.Sprintf("%v", value),
3635
Type: rType,
36+
Value: value,
3737
}
3838
}
3939

40+
func newUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError {
41+
return newUnmarshalTypeStrError(fmt.Sprintf("%v (%T)", value, value), rType)
42+
}
43+
4044
func (e UnmarshalTypeError) Error() string {
41-
return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type.String())
45+
return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type)
4246
}

reader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func TestBrokenDoubleDatabase(t *testing.T) {
594594
expected := newInvalidDatabaseError(
595595
"the MaxMind DB file's data section contains bad data (float 64 size of 2)",
596596
)
597-
assert.Equal(t, expected, err)
597+
require.ErrorAs(t, err, &expected)
598598
require.NoError(t, reader.Close(), "error on close")
599599
}
600600

0 commit comments

Comments
 (0)