1
0
mirror of synced 2024-11-22 04:46:05 +03:00

add unmarshal error

This commit is contained in:
Tyschitskaya Maria 2022-01-26 14:35:38 +03:00
parent 1fc20f8bb5
commit cd6859b074
5 changed files with 115 additions and 3 deletions

View File

@ -198,7 +198,7 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro
err = json.Unmarshal(content, &p)
if err != nil {
return p, err
return p, NewUnmarshalError().WithReaderContent(content)
}
if p == *new(Profile) {

View File

@ -89,7 +89,7 @@ func checkFacebookError(r io.Reader) error {
qr := QueryResponse{}
err = json.NewDecoder(r).Decode(&qr)
if err != nil {
return xerrors.Errorf("json unmarshal error: %w", err)
return NewUnmarshalError().WithReader(r)
}
if qr.Error != nil {
return xerrors.Errorf("facebook error: %w", qr.Error)
@ -102,7 +102,7 @@ func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) {
qr := QueryResponse{}
err := json.NewDecoder(r).Decode(&qr)
if err != nil {
return qr, xerrors.Errorf("json unmarshal error: %w", err)
return qr, NewUnmarshalError().WithReader(r)
}
if qr.Error != nil {
return qr, xerrors.Errorf("facebook error: %w", qr.Error)

View File

@ -1,7 +1,9 @@
package messenger
import (
"bytes"
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
@ -15,3 +17,15 @@ func Test_MarshalStructuredMessageElement(t *testing.T) {
require.NoError(t, err)
assert.JSONEq(t, string(data), `{"image_url":"", "subtitle":"", "title": "Title"}`)
}
func TestResponse_checkFacebookError_UnmarshalError(t *testing.T) {
r := bytes.NewReader([]byte("test"))
err := checkFacebookError(r)
assert.True(t, errors.Is(err, ErrUnmarshal))
}
func TestResponse_getFacebookQueryResponse_UnmarshalError(t *testing.T) {
r := bytes.NewReader([]byte("test"))
_, err := getFacebookQueryResponse(r)
assert.True(t, errors.Is(err, ErrUnmarshal))
}

46
unmarshal_error.go Normal file
View File

@ -0,0 +1,46 @@
package messenger
import (
"bytes"
"errors"
"fmt"
"io"
)
var ErrUnmarshal = errors.New("unmarshal error")
type UnmarshalError struct {
Content io.Reader
Err error
}
func (u *UnmarshalError) Error() string {
content, err := io.ReadAll(u.Content)
if err != nil {
content = []byte("[can not read content]")
}
return fmt.Sprintf("can not unmarshal content: %s", string(content))
}
func (u *UnmarshalError) Unwrap() error {
return u.Err
}
func NewUnmarshalError() *UnmarshalError {
return &UnmarshalError{Err: ErrUnmarshal}
}
func (u *UnmarshalError) WithReader(content io.Reader) *UnmarshalError {
u.Content = content
return u
}
func (u *UnmarshalError) WithReaderContent(content []byte) *UnmarshalError {
u.Content = bytes.NewReader(content)
return u
}
func (u *UnmarshalError) WithErr(err error) *UnmarshalError {
u.Err = err
return u
}

52
unmarshal_error_test.go Normal file
View File

@ -0,0 +1,52 @@
package messenger
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewUnmarshalError(t *testing.T) {
err := NewUnmarshalError()
assert.True(t, errors.Is(err, ErrUnmarshal))
}
func TestUnmarshalError_Error(t *testing.T) {
content := []byte("test content")
actual := NewUnmarshalError().WithReaderContent(content).Error()
expected := "can not unmarshal content: test content"
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_Unwrap(t *testing.T) {
actual := NewUnmarshalError().Unwrap()
expected := ErrUnmarshal
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_WithReaderContent(t *testing.T) {
content := []byte("test content")
reader := bytes.NewReader(content)
actual := NewUnmarshalError().WithReaderContent(content)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader}
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_WithReader(t *testing.T) {
content := []byte("test content")
reader := bytes.NewReader(content)
actual := NewUnmarshalError().WithReader(reader)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader}
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_WithErr(t *testing.T) {
err := errors.New("some error")
actual := NewUnmarshalError().WithErr(err)
expected := &UnmarshalError{Err: err}
assert.Equal(t, expected, actual)
}