2022-01-26 14:35:38 +03:00
|
|
|
package messenger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrUnmarshal = errors.New("unmarshal error")
|
|
|
|
|
|
|
|
type UnmarshalError struct {
|
2022-01-28 12:24:58 +03:00
|
|
|
Content []byte
|
|
|
|
ErrorText string
|
|
|
|
Err error
|
2022-01-26 14:35:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UnmarshalError) Error() string {
|
2022-01-28 12:24:58 +03:00
|
|
|
return fmt.Sprintf("can not unmarshal content: %s; error: %s", string(u.Content), u.ErrorText)
|
2022-01-26 14:35:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UnmarshalError) Unwrap() error {
|
|
|
|
return u.Err
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:24:58 +03:00
|
|
|
func NewUnmarshalError(err error) *UnmarshalError {
|
|
|
|
return &UnmarshalError{
|
|
|
|
Err: ErrUnmarshal,
|
|
|
|
ErrorText: err.Error(),
|
|
|
|
}
|
2022-01-26 14:35:38 +03:00
|
|
|
}
|
|
|
|
|
2022-01-28 12:24:58 +03:00
|
|
|
func (u *UnmarshalError) WithReader(reader io.Reader) *UnmarshalError {
|
|
|
|
content, _ := io.ReadAll(reader)
|
2022-01-26 14:35:38 +03:00
|
|
|
u.Content = content
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:24:58 +03:00
|
|
|
func (u *UnmarshalError) WithContent(content []byte) *UnmarshalError {
|
|
|
|
u.Content = content
|
2022-01-26 14:35:38 +03:00
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UnmarshalError) WithErr(err error) *UnmarshalError {
|
|
|
|
u.Err = err
|
|
|
|
return u
|
|
|
|
}
|