From 51df7376983c120ef123426b71cee236735fd88f Mon Sep 17 00:00:00 2001 From: Sebastian Nowicki Date: Tue, 21 Mar 2017 12:01:26 +0100 Subject: [PATCH] Handle string errors in Decode() It's possible that Decode() panics with a plain `string`. The `string` is type asserted to `error` which fails due to `string` not implementing the `error` interface. Example panic with string: reflect: reflect.Value.Set using value obtained using unexported field --- result.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/result.go b/result.go index 099c40c..a5f9971 100644 --- a/result.go +++ b/result.go @@ -10,6 +10,7 @@ package facebook import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" "reflect" @@ -216,7 +217,17 @@ func (res Result) Decode(v interface{}) (err error) { panic(r) } - err = r.(error) + if errStr, ok := r.(string); ok { + err = errors.New(errStr) + return + } + + if errErr, ok := r.(error); ok { + err = errErr + return + } + + panic(r) } }()