1
0
mirror of synced 2024-11-22 12:56:06 +03:00
messenger/unmarshal_error.go

48 lines
885 B
Go
Raw Normal View History

2022-01-26 14:35:38 +03:00
package messenger
import (
"bytes"
"errors"
"fmt"
"io"
2022-01-27 14:56:47 +03:00
"io/ioutil"
2022-01-26 14:35:38 +03:00
)
var ErrUnmarshal = errors.New("unmarshal error")
type UnmarshalError struct {
Content io.Reader
Err error
}
func (u *UnmarshalError) Error() string {
2022-01-27 14:56:47 +03:00
content, err := ioutil.ReadAll(u.Content)
2022-01-26 14:35:38 +03:00
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
}