1
0
mirror of synced 2024-11-22 04:46:05 +03:00
messenger/unmarshal_error.go
2022-01-28 12:24:58 +03:00

47 lines
857 B
Go

package messenger
import (
"errors"
"fmt"
"io"
)
var ErrUnmarshal = errors.New("unmarshal error")
type UnmarshalError struct {
Content []byte
ErrorText string
Err error
}
func (u *UnmarshalError) Error() string {
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(err error) *UnmarshalError {
return &UnmarshalError{
Err: ErrUnmarshal,
ErrorText: err.Error(),
}
}
func (u *UnmarshalError) WithReader(reader io.Reader) *UnmarshalError {
content, _ := io.ReadAll(reader)
u.Content = content
return u
}
func (u *UnmarshalError) WithContent(content []byte) *UnmarshalError {
u.Content = content
return u
}
func (u *UnmarshalError) WithErr(err error) *UnmarshalError {
u.Err = err
return u
}