1
0
mirror of synced 2024-11-22 04:46:05 +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) err = json.Unmarshal(content, &p)
if err != nil { if err != nil {
return p, NewUnmarshalError().WithReaderContent(content) return p, NewUnmarshalError(err).WithContent(content)
} }
if p == *new(Profile) { if p == *new(Profile) {

View File

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

View File

@ -1,43 +1,42 @@
package messenger package messenger
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
) )
var ErrUnmarshal = errors.New("unmarshal error") var ErrUnmarshal = errors.New("unmarshal error")
type UnmarshalError struct { type UnmarshalError struct {
Content io.Reader Content []byte
ErrorText string
Err error Err error
} }
func (u *UnmarshalError) Error() string { func (u *UnmarshalError) Error() string {
content, err := ioutil.ReadAll(u.Content) return fmt.Sprintf("can not unmarshal content: %s; error: %s", string(u.Content), u.ErrorText)
if err != nil {
content = []byte("[can not read content]")
}
return fmt.Sprintf("can not unmarshal content: %s", string(content))
} }
func (u *UnmarshalError) Unwrap() error { func (u *UnmarshalError) Unwrap() error {
return u.Err return u.Err
} }
func NewUnmarshalError() *UnmarshalError { func NewUnmarshalError(err error) *UnmarshalError {
return &UnmarshalError{Err: ErrUnmarshal} 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 u.Content = content
return u return u
} }
func (u *UnmarshalError) WithReaderContent(content []byte) *UnmarshalError { func (u *UnmarshalError) WithContent(content []byte) *UnmarshalError {
u.Content = bytes.NewReader(content) u.Content = content
return u return u
} }

View File

@ -9,44 +9,49 @@ import (
) )
func TestNewUnmarshalError(t *testing.T) { func TestNewUnmarshalError(t *testing.T) {
err := NewUnmarshalError() err := errors.New("some error")
assert.True(t, errors.Is(err, ErrUnmarshal)) unmarshalError := NewUnmarshalError(err)
assert.True(t, errors.Is(unmarshalError, ErrUnmarshal))
} }
func TestUnmarshalError_Error(t *testing.T) { func TestUnmarshalError_Error(t *testing.T) {
err := errors.New("some error")
content := []byte("test content") content := []byte("test content")
actual := NewUnmarshalError().WithReaderContent(content).Error() actual := NewUnmarshalError(err).WithContent(content).Error()
expected := "can not unmarshal content: test content" expected := "can not unmarshal content: test content; error: some error"
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
func TestUnmarshalError_Unwrap(t *testing.T) { func TestUnmarshalError_Unwrap(t *testing.T) {
actual := NewUnmarshalError().Unwrap() err := errors.New("some error")
actual := NewUnmarshalError(err).Unwrap()
expected := ErrUnmarshal expected := ErrUnmarshal
assert.Equal(t, expected, actual) 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") content := []byte("test content")
reader := bytes.NewReader(content)
actual := NewUnmarshalError().WithReaderContent(content) actual := NewUnmarshalError(err).WithContent(content)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader} expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()}
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
func TestUnmarshalError_WithReader(t *testing.T) { func TestUnmarshalError_WithReader(t *testing.T) {
err := errors.New("some error")
content := []byte("test content") content := []byte("test content")
reader := bytes.NewReader(content) reader := bytes.NewReader(content)
actual := NewUnmarshalError().WithReader(reader) actual := NewUnmarshalError(err).WithReader(reader)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: reader} expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()}
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
func TestUnmarshalError_WithErr(t *testing.T) { func TestUnmarshalError_WithErr(t *testing.T) {
err := errors.New("some error") someError := errors.New("some error")
actual := NewUnmarshalError().WithErr(err) otherError := errors.New("other error")
expected := &UnmarshalError{Err: err} actual := NewUnmarshalError(someError).WithErr(otherError)
expected := &UnmarshalError{Err: otherError, ErrorText: someError.Error()}
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }