bencode: Improve UnmarshalTypeError string and list parsing error context

Helps with #297.
This commit is contained in:
Matt Joiner 2019-06-13 12:35:11 +10:00
parent 53be473486
commit d4584c2ca6
2 changed files with 4 additions and 5 deletions

View File

@ -46,8 +46,7 @@ type UnmarshalTypeError struct {
} }
func (e *UnmarshalTypeError) Error() string { func (e *UnmarshalTypeError) Error() string {
return "bencode: value (" + e.Value + ") is not appropriate for type: " + return fmt.Sprintf("cannot unmarshal a bencode %s into a %s", e.Value, e.Type)
e.Type.String()
} }
// Unmarshaler tried to write to an unexported (therefore unwritable) field. // Unmarshaler tried to write to an unexported (therefore unwritable) field.

View File

@ -346,10 +346,10 @@ func (d *Decoder) parseList(v reflect.Value) error {
switch v.Kind() { switch v.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
panic(&UnmarshalTypeError{ return &UnmarshalTypeError{
Value: "array", Value: "list",
Type: v.Type(), Type: v.Type(),
}) }
} }
i := 0 i := 0