From 79c1bab4153473be94977c8ba98a82d2af8f86b6 Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Fri, 14 May 2021 11:15:37 +0200 Subject: [PATCH] Now logging json when unmarshal fails --- http.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 72a3b0f..59a2939 100644 --- a/http.go +++ b/http.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net" "net/http" "os" @@ -244,8 +245,16 @@ func (s *HTTPSender) SendJSON(o HTTPSendJSONOptions) (err error) { // Unmarshal body out if o.BodyOut != nil { - if err = json.NewDecoder(resp.Body).Decode(o.BodyOut); err != nil { - err = fmt.Errorf("astikit: unmarshaling failed: %w", err) + // Read all + var b []byte + if b, err = ioutil.ReadAll(resp.Body); err != nil { + err = fmt.Errorf("astikit: reading all failed: %w", err) + return + } + + // Unmarshal + if err = json.Unmarshal(b, o.BodyOut); err != nil { + err = fmt.Errorf("astikit: unmarshaling failed: %w (json: %s)", err, b) return } }