diff --git a/messenger.go b/messenger.go index 4ef799f..7e574df 100644 --- a/messenger.go +++ b/messenger.go @@ -198,7 +198,7 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro err = json.Unmarshal(content, &p) if err != nil { - return p, NewUnmarshalError().WithReaderContent(content) + return p, NewUnmarshalError(err).WithContent(content) } if p == *new(Profile) { diff --git a/response.go b/response.go index a7a0502..d2db26c 100644 --- a/response.go +++ b/response.go @@ -90,7 +90,7 @@ func checkFacebookError(r io.Reader) error { decoder := json.NewDecoder(r) err = decoder.Decode(&qr) if err != nil { - return NewUnmarshalError().WithReader(decoder.Buffered()) + return NewUnmarshalError(err).WithReader(decoder.Buffered()) } if qr.Error != nil { return xerrors.Errorf("facebook error: %w", qr.Error) @@ -103,7 +103,7 @@ func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) { qr := QueryResponse{} decoder := json.NewDecoder(r) if err := decoder.Decode(&qr); err != nil { - return qr, NewUnmarshalError().WithReader(decoder.Buffered()) + return qr, NewUnmarshalError(err).WithReader(decoder.Buffered()) } if qr.Error != nil { return qr, xerrors.Errorf("facebook error: %w", qr.Error) diff --git a/unmarshal_error.go b/unmarshal_error.go index 1163f9c..ddcb816 100644 --- a/unmarshal_error.go +++ b/unmarshal_error.go @@ -1,43 +1,42 @@ package messenger import ( - "bytes" "errors" "fmt" "io" - "io/ioutil" ) var ErrUnmarshal = errors.New("unmarshal error") type UnmarshalError struct { - Content io.Reader - Err error + Content []byte + ErrorText string + Err error } func (u *UnmarshalError) Error() string { - content, err := ioutil.ReadAll(u.Content) - if err != nil { - content = []byte("[can not read content]") - } - return fmt.Sprintf("can not unmarshal content: %s", string(content)) + return fmt.Sprintf("can not unmarshal content: %s; error: %s", string(u.Content), u.ErrorText) } func (u *UnmarshalError) Unwrap() error { return u.Err } -func NewUnmarshalError() *UnmarshalError { - return &UnmarshalError{Err: ErrUnmarshal} +func NewUnmarshalError(err error) *UnmarshalError { + return &UnmarshalError{ + Err: ErrUnmarshal, + ErrorText: err.Error(), + } } -func (u *UnmarshalError) WithReader(content io.Reader) *UnmarshalError { +func (u *UnmarshalError) WithReader(reader io.Reader) *UnmarshalError { + content, _ := io.ReadAll(reader) u.Content = content return u } -func (u *UnmarshalError) WithReaderContent(content []byte) *UnmarshalError { - u.Content = bytes.NewReader(content) +func (u *UnmarshalError) WithContent(content []byte) *UnmarshalError { + u.Content = content return u } diff --git a/unmarshal_error_test.go b/unmarshal_error_test.go index 1d799df..f8e160c 100644 --- a/unmarshal_error_test.go +++ b/unmarshal_error_test.go @@ -9,44 +9,49 @@ import ( ) func TestNewUnmarshalError(t *testing.T) { - err := NewUnmarshalError() - assert.True(t, errors.Is(err, ErrUnmarshal)) + err := errors.New("some error") + unmarshalError := NewUnmarshalError(err) + assert.True(t, errors.Is(unmarshalError, ErrUnmarshal)) } func TestUnmarshalError_Error(t *testing.T) { + err := errors.New("some error") content := []byte("test content") - actual := NewUnmarshalError().WithReaderContent(content).Error() - expected := "can not unmarshal content: test content" + actual := NewUnmarshalError(err).WithContent(content).Error() + expected := "can not unmarshal content: test content; error: some error" assert.Equal(t, expected, actual) } func TestUnmarshalError_Unwrap(t *testing.T) { - actual := NewUnmarshalError().Unwrap() + err := errors.New("some error") + actual := NewUnmarshalError(err).Unwrap() expected := ErrUnmarshal assert.Equal(t, expected, actual) } -func TestUnmarshalError_WithReaderContent(t *testing.T) { +func TestUnmarshalError_WithContent(t *testing.T) { + err := errors.New("some error") content := []byte("test content") - reader := bytes.NewReader(content) - actual := NewUnmarshalError().WithReaderContent(content) - expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader} + actual := NewUnmarshalError(err).WithContent(content) + expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()} assert.Equal(t, expected, actual) } func TestUnmarshalError_WithReader(t *testing.T) { + err := errors.New("some error") content := []byte("test content") reader := bytes.NewReader(content) - actual := NewUnmarshalError().WithReader(reader) - expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader} + actual := NewUnmarshalError(err).WithReader(reader) + expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()} assert.Equal(t, expected, actual) } func TestUnmarshalError_WithErr(t *testing.T) { - err := errors.New("some error") - actual := NewUnmarshalError().WithErr(err) - expected := &UnmarshalError{Err: err} + someError := errors.New("some error") + otherError := errors.New("other error") + actual := NewUnmarshalError(someError).WithErr(otherError) + expected := &UnmarshalError{Err: otherError, ErrorText: someError.Error()} assert.Equal(t, expected, actual) }