1
0
mirror of synced 2024-11-25 06:16:07 +03:00
messenger/unmarshal_error.go

48 lines
874 B
Go
Raw Normal View History

2022-01-26 14:35:38 +03:00
package messenger
import (
"errors"
"fmt"
"io"
2022-01-28 12:28:24 +03:00
"io/ioutil"
2022-01-26 14:35:38 +03:00
)
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 {
2022-01-28 12:28:24 +03:00
content, _ := ioutil.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
}