1
0
mirror of synced 2024-11-21 12:26:08 +03:00

buffer from decoder in unmarshal error

This commit is contained in:
Tyschitskaya Maria 2022-01-27 17:17:20 +03:00
parent f24f4d512e
commit c1e9fd594d
2 changed files with 10 additions and 7 deletions

View File

@ -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)

View File

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