refactor(errors): change errors

This commit is contained in:
pyihe
2021-07-13 17:44:54 +08:00
parent a20ace4425
commit c5eb321de9
7 changed files with 122 additions and 81 deletions

31
errors/code.go Normal file
View File

@@ -0,0 +1,31 @@
package errors
import (
"fmt"
)
const (
DefaultErrCode ErrorCode = -1
)
type ErrorCode int64
func (ec ErrorCode) Int() int {
return int(ec)
}
func (ec ErrorCode) Int64() int64 {
return int64(ec)
}
func (ec ErrorCode) Int32() int32 {
return int32(ec)
}
func (ec ErrorCode) ToString() string {
return fmt.Sprintf("%d", ec)
}
func (ec ErrorCode) Equal(target ErrorCode) bool {
return ec == target
}

83
errors/err.go Normal file
View File

@@ -0,0 +1,83 @@
package errors
import (
"errors"
"fmt"
)
type Error interface {
Error() string // 包含code和msg
Code() ErrorCode // 错误码
Desc() string // 错误描述
Data() interface{} // 附加信息
WithData(interface{}) // 给错误添加附加信息
Is(target error) bool
As(target interface{}) bool
}
type _err struct {
data interface{} // 如果需要包含特定的数据
code ErrorCode // 错误码
msg string // 错误描述
}
func New(err string) Error {
return &_err{
code: DefaultErrCode,
msg: err,
}
}
func NewWithCode(err string, code ErrorCode) Error {
return &_err{
code: code,
msg: err,
}
}
func (e *_err) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf("err: %d-%s", e.code, e.msg)
}
func (e *_err) Code() ErrorCode {
if e == nil {
return 0
}
return e.code
}
func (e *_err) Desc() string {
if e == nil {
return ""
}
return e.msg
}
func (e *_err) Data() interface{} {
if e == nil {
return nil
}
return e.data
}
func (e *_err) WithData(data interface{}) {
if data == nil || e == nil {
return
}
e.data = data
}
func (e *_err) Is(target error) bool {
return errors.Is(e, target)
}
func (e *_err) As(target interface{}) bool {
return errors.As(e, target)
}
func (e *_err) Unwrap() error {
return errors.Unwrap(e)
}

View File

@@ -1,73 +0,0 @@
package errors
import (
"fmt"
)
const (
ErrorCodeFail = 100000 //默认错误代码
)
type Error interface {
Error() string
Code() int
Message() string
WithCode(code int) Error
WithMessage(msg string) Error
}
type myError struct {
code int
message string
}
func NewError(code int, message string) Error {
return &myError{
code: code,
message: message,
}
}
func Errorf(m string, args ...interface{}) Error {
e := &myError{}
e.message = fmt.Sprintf(m, args...)
return e
}
func (m *myError) WithCode(code int) Error {
if m == nil {
return nil
}
m.code = code
return m
}
func (m *myError) WithMessage(msg string) Error {
if m == nil {
return nil
}
m.message = msg
return m
}
func (m *myError) Error() string {
if m == nil {
return ""
}
return fmt.Sprintf("Code: %d, Message: %s", m.code, m.message)
}
func (m *myError) Message() string {
if m == nil {
return ""
}
return m.message
}
func (m *myError) Code() int {
if m != nil {
return m.code
}
return 0
}

View File

@@ -11,8 +11,8 @@ import (
)
var (
ErrInvalidUrl = errors.NewError(errors.ErrorCodeFail, "url must start with 'http'")
ErrInvalidEncoder = errors.NewError(errors.ErrorCodeFail, "invalid encoder")
ErrInvalidUrl = errors.New("url must start with 'http'")
ErrInvalidEncoder = errors.New("invalid encoder")
)
// Get

View File

@@ -7,10 +7,10 @@ import (
)
var (
ErrInvalidKey = errors.NewError(errors.ErrorCodeFail, "invalid key")
ErrInvalidEncoder = errors.NewError(errors.ErrorCodeFail, "not figure encoder")
ErrInvalidConn = errors.NewError(errors.ErrorCodeFail, "invalid redis conn")
ErrInvalidParamNum = errors.NewError(errors.ErrorCodeFail, "invalid param num")
ErrInvalidKey = errors.New("invalid key")
ErrInvalidEncoder = errors.New("not figure encoder")
ErrInvalidConn = errors.New("invalid redis conn")
ErrInvalidParamNum = errors.New("invalid param num")
)
type myRedisConn struct {

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"time"
"github.com/pyihe/util/encoding"
"github.com/pyihe/go-pkg/encoding"
"github.com/garyburd/redigo/redis"
)

View File

@@ -54,7 +54,7 @@ func Notify(onNotify func()) {
// Contain 判断src中是否有元素ele
func Contain(src interface{}, ele interface{}) bool {
switch reflect.TypeOf(src).Kind() {
case reflect.Slice:
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(src)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(ele, s.Index(i).Interface()) {