You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
714 B
Go
44 lines
714 B
Go
package protocol
|
|
|
|
import "fmt"
|
|
|
|
// Error 协议错误
|
|
type Error struct {
|
|
Message string
|
|
Cause error
|
|
}
|
|
|
|
// NewError 创建新错误
|
|
func NewError(message string) error {
|
|
return &Error{
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
// NewErrorf 创建格式化错误
|
|
func NewErrorf(format string, args ...interface{}) error {
|
|
return &Error{
|
|
Message: fmt.Sprintf(format, args...),
|
|
}
|
|
}
|
|
|
|
// WithCause 设置原因
|
|
func (e *Error) WithCause(cause error) error {
|
|
e.Cause = cause
|
|
return e
|
|
}
|
|
|
|
// Error 实现error接口
|
|
func (e *Error) Error() string {
|
|
if e.Cause != nil {
|
|
return fmt.Sprintf("%s: %v", e.Message, e.Cause)
|
|
}
|
|
return e.Message
|
|
}
|
|
|
|
// Unwrap 实现errors.Unwrap
|
|
func (e *Error) Unwrap() error {
|
|
return e.Cause
|
|
}
|
|
|