From d4584c2ca60328458db24edda2a2bc470ccd4228 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 13 Jun 2019 12:35:11 +1000 Subject: [PATCH] bencode: Improve UnmarshalTypeError string and list parsing error context Helps with #297. --- bencode/api.go | 3 +-- bencode/decode.go | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/bencode/api.go b/bencode/api.go index 15687a8c..3e3c1633 100644 --- a/bencode/api.go +++ b/bencode/api.go @@ -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. diff --git a/bencode/decode.go b/bencode/decode.go index 03488999..13e63f71 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -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 -- 2.48.1