1
0
mirror of synced 2024-11-21 20:36:06 +03:00

unmarshal error text

This commit is contained in:
Tyschitskaya Maria 2022-01-28 12:24:58 +03:00
parent c1e9fd594d
commit 76d0d601a0
4 changed files with 35 additions and 31 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, NewUnmarshalError().WithReaderContent(content)
return p, NewUnmarshalError(err).WithContent(content)
}
if p == *new(Profile) {

View File

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

View File

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

View File

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