diff --git a/decoder.go b/decoder.go index dd0f9ba..435591e 100644 --- a/decoder.go +++ b/decoder.go @@ -2,6 +2,7 @@ package maxminddb import ( "encoding/binary" + "fmt" "math" "math/big" "reflect" @@ -418,7 +419,7 @@ func (d *decoder) unmarshalMap( result = indirect(result) switch result.Kind() { default: - return 0, newUnmarshalTypeError("map", result.Type()) + return 0, newUnmarshalTypeStrError("map", result.Type()) case reflect.Struct: return d.decodeStruct(size, offset, result, depth) case reflect.Map: @@ -430,7 +431,7 @@ func (d *decoder) unmarshalMap( result.Set(rv) return newOffset, err } - return 0, newUnmarshalTypeError("map", result.Type()) + return 0, newUnmarshalTypeStrError("map", result.Type()) } } @@ -465,7 +466,7 @@ func (d *decoder) unmarshalSlice( return newOffset, err } } - return 0, newUnmarshalTypeError("array", result.Type()) + return 0, newUnmarshalTypeStrError("array", result.Type()) } func (d *decoder) unmarshalString(size, offset uint, result reflect.Value) (uint, error) { @@ -615,7 +616,7 @@ func (d *decoder) decodeMap( offset, err = d.decode(offset, elemValue, depth) if err != nil { - return 0, err + return 0, fmt.Errorf("decoding value for %s: %w", key, err) } keyValue.SetString(string(key)) @@ -772,7 +773,7 @@ func (d *decoder) decodeStruct( offset, err = d.decode(offset, result.Field(j), depth) if err != nil { - return 0, err + return 0, fmt.Errorf("decoding value for %s: %w", key, err) } } return offset, nil diff --git a/errors.go b/errors.go index aeba906..f141f61 100644 --- a/errors.go +++ b/errors.go @@ -30,13 +30,17 @@ type UnmarshalTypeError struct { Value string } -func newUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError { +func newUnmarshalTypeStrError(value string, rType reflect.Type) UnmarshalTypeError { return UnmarshalTypeError{ - Value: fmt.Sprintf("%v", value), Type: rType, + Value: value, } } +func newUnmarshalTypeError(value any, rType reflect.Type) UnmarshalTypeError { + return newUnmarshalTypeStrError(fmt.Sprintf("%v (%T)", value, value), rType) +} + func (e UnmarshalTypeError) Error() string { - return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type.String()) + return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type) } diff --git a/reader_test.go b/reader_test.go index fe468d2..c9d287d 100644 --- a/reader_test.go +++ b/reader_test.go @@ -594,7 +594,7 @@ func TestBrokenDoubleDatabase(t *testing.T) { expected := newInvalidDatabaseError( "the MaxMind DB file's data section contains bad data (float 64 size of 2)", ) - assert.Equal(t, expected, err) + require.ErrorAs(t, err, &expected) require.NoError(t, reader.Close(), "error on close") }