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 {
return "bencode: value (" + e.Value + ") is not appropriate for type: " +
e.Type.String()
return fmt.Sprintf("cannot unmarshal a bencode %s into a %s", e.Value, e.Type)
}
// 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() {
case reflect.Array, reflect.Slice:
default:
panic(&UnmarshalTypeError{
Value: "array",
return &UnmarshalTypeError{
Value: "list",
Type: v.Type(),
})
}
}
i := 0