Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages when unmarshaling map values #119

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package maxminddb

import (
"encoding/binary"
"fmt"
"math"
"math/big"
"reflect"
Expand Down Expand Up @@ -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:
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Loading