Skip to content

Commit

Permalink
Merge pull request #119 from oschwald/greg/improve-errors
Browse files Browse the repository at this point in the history
Improve error messages when unmarshaling map values
  • Loading branch information
oschwald committed Jun 2, 2024
2 parents 64f043c + 32f205a commit c1361f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
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

0 comments on commit c1361f0

Please sign in to comment.