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") }