From c1e9fd594dd2306e803a23b0f7e0e26f704c437d Mon Sep 17 00:00:00 2001 From: Tyschitskaya Maria Date: Thu, 27 Jan 2022 17:17:20 +0300 Subject: [PATCH] buffer from decoder in unmarshal error --- response.go | 11 ++++++----- response_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/response.go b/response.go index d5bca94..a7a0502 100644 --- a/response.go +++ b/response.go @@ -87,9 +87,10 @@ func checkFacebookError(r io.Reader) error { var err error qr := QueryResponse{} - err = json.NewDecoder(r).Decode(&qr) + decoder := json.NewDecoder(r) + err = decoder.Decode(&qr) if err != nil { - return NewUnmarshalError().WithReader(r) + return NewUnmarshalError().WithReader(decoder.Buffered()) } if qr.Error != nil { return xerrors.Errorf("facebook error: %w", qr.Error) @@ -100,9 +101,9 @@ func checkFacebookError(r io.Reader) error { func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) { qr := QueryResponse{} - err := json.NewDecoder(r).Decode(&qr) - if err != nil { - return qr, NewUnmarshalError().WithReader(r) + decoder := json.NewDecoder(r) + if err := decoder.Decode(&qr); err != nil { + return qr, NewUnmarshalError().WithReader(decoder.Buffered()) } if qr.Error != nil { return qr, xerrors.Errorf("facebook error: %w", qr.Error) diff --git a/response_test.go b/response_test.go index f15663e..258b639 100644 --- a/response_test.go +++ b/response_test.go @@ -19,13 +19,15 @@ func Test_MarshalStructuredMessageElement(t *testing.T) { } func TestResponse_checkFacebookError_UnmarshalError(t *testing.T) { - r := bytes.NewReader([]byte("test")) + r := bytes.NewReader([]byte("test error text")) err := checkFacebookError(r) assert.True(t, errors.Is(err, ErrUnmarshal)) + assert.Contains(t, err.Error(), "test error text") } func TestResponse_getFacebookQueryResponse_UnmarshalError(t *testing.T) { - r := bytes.NewReader([]byte("test")) + r := bytes.NewReader([]byte("test error text")) _, err := getFacebookQueryResponse(r) assert.True(t, errors.Is(err, ErrUnmarshal)) + assert.Contains(t, err.Error(), "test error text") }