Skip to content

Commit e970a48

Browse files
committed
Add type information to unmarshaling error messages
1 parent affa445 commit e970a48

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

decoder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func (d *decoder) unmarshalMap(
418418
result = indirect(result)
419419
switch result.Kind() {
420420
default:
421-
return 0, newUnmarshalTypeError("map", result.Type())
421+
return 0, newUnmarshalTypeStrError("map", result.Type())
422422
case reflect.Struct:
423423
return d.decodeStruct(size, offset, result, depth)
424424
case reflect.Map:
@@ -430,7 +430,7 @@ func (d *decoder) unmarshalMap(
430430
result.Set(rv)
431431
return newOffset, err
432432
}
433-
return 0, newUnmarshalTypeError("map", result.Type())
433+
return 0, newUnmarshalTypeStrError("map", result.Type())
434434
}
435435
}
436436

@@ -465,7 +465,7 @@ func (d *decoder) unmarshalSlice(
465465
return newOffset, err
466466
}
467467
}
468-
return 0, newUnmarshalTypeError("array", result.Type())
468+
return 0, newUnmarshalTypeStrError("array", result.Type())
469469
}
470470

471471
func (d *decoder) unmarshalString(size, offset uint, result reflect.Value) (uint, error) {

errors.go

Lines changed: 7 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)