From e970a4811fba7d740087f20dac822357e2d63b2f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 20 Sep 2023 13:54:42 -0700 Subject: [PATCH] Add type information to unmarshaling error messages --- decoder.go | 6 +++--- errors.go | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/decoder.go b/decoder.go index dd0f9ba..8774531 100644 --- a/decoder.go +++ b/decoder.go @@ -418,7 +418,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 +430,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 +465,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) { 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) }