From f6ffb68c432bc78ff5a86e4ffd0fed92848d2fff Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 4 Mar 2024 18:51:36 +0100 Subject: [PATCH] feat(client): show response body on json parsing error --- pkg/client/client.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index c47b526..0b11092 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "github.com/pkg/errors" @@ -95,12 +96,15 @@ func (c *Client) apiDo(ctx context.Context, method string, path string, payload defer res.Body.Close() - decoder := json.NewDecoder(res.Body) - - if err := decoder.Decode(&response); err != nil { + data, err := io.ReadAll(res.Body) + if err != nil { return errors.WithStack(err) } + if err := json.Unmarshal(data, &response); err != nil { + return errors.Wrapf(err, "could not parse json: got '%s'", data) + } + return nil }