]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: Improve UnmarshalTypeError string and list parsing error context
authorMatt Joiner <anacrolix@gmail.com>
Thu, 13 Jun 2019 02:35:11 +0000 (12:35 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 13 Jun 2019 02:35:11 +0000 (12:35 +1000)
Helps with #297.

bencode/api.go
bencode/decode.go

index 15687a8ce31e14ebb6726a1d11423c0ae95dcd6d..3e3c1633b39d63f02ebbdf9b8fb625bed17bc616 100644 (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.
index 03488999f756d92524e5cb2b7d3f8926194949b8..13e63f71e74afc9ba5829beb380fbb77bba5bdbe 100644 (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