From c5eb321de9aed7558ce41550fbbafa1ce2d38e45 Mon Sep 17 00:00:00 2001 From: pyihe <785131182@qq.com> Date: Tue, 13 Jul 2021 17:44:54 +0800 Subject: [PATCH] refactor(errors): change errors --- errors/code.go | 31 ++++++++++++++++++ errors/err.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ errors/errors.go | 73 ------------------------------------------ https/http.go | 4 +-- redis/conn.go | 8 ++--- redis/pool.go | 2 +- utils/utils.go | 2 +- 7 files changed, 122 insertions(+), 81 deletions(-) create mode 100644 errors/code.go create mode 100644 errors/err.go delete mode 100644 errors/errors.go diff --git a/errors/code.go b/errors/code.go new file mode 100644 index 0000000..543125c --- /dev/null +++ b/errors/code.go @@ -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 +} diff --git a/errors/err.go b/errors/err.go new file mode 100644 index 0000000..d76c506 --- /dev/null +++ b/errors/err.go @@ -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) +} diff --git a/errors/errors.go b/errors/errors.go deleted file mode 100644 index 4395a84..0000000 --- a/errors/errors.go +++ /dev/null @@ -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 -} diff --git a/https/http.go b/https/http.go index 072e455..b551bcd 100644 --- a/https/http.go +++ b/https/http.go @@ -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 diff --git a/redis/conn.go b/redis/conn.go index 73941fe..1be9bcb 100644 --- a/redis/conn.go +++ b/redis/conn.go @@ -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 { diff --git a/redis/pool.go b/redis/pool.go index 48c5099..190e1e8 100644 --- a/redis/pool.go +++ b/redis/pool.go @@ -4,7 +4,7 @@ import ( "fmt" "time" - "github.com/pyihe/util/encoding" + "github.com/pyihe/go-pkg/encoding" "github.com/garyburd/redigo/redis" ) diff --git a/utils/utils.go b/utils/utils.go index 0387194..c7c6818 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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()) {