github.com/OrigamiWang/msd/micro@v0.0.0-20240229032328-b62246268db9/model/errx/error.go (about) 1 package errx 2 3 import ( 4 "fmt" 5 ) 6 7 type ErrX interface { 8 Code() int 9 Error() string 10 String() string 11 } 12 13 type errX struct { 14 code int 15 msg string 16 } 17 18 func (e *errX) Code() int { 19 return e.code 20 } 21 22 func (e *errX) Error() string { 23 return e.msg 24 } 25 26 func (e errX) String() string { 27 return fmt.Sprintf("&errX{code:%d;msg:\"%s\"}", e.code, e.msg) 28 } 29 30 func New(code int, msg string) ErrX { 31 return &errX{code, msg} 32 } 33 34 func IsErrX(e interface{}) bool { 35 _, ok := e.(ErrX) 36 return ok 37 } 38 39 func IsError(e interface{}) bool { 40 _, ok := e.(error) 41 return ok 42 } 43 func NewErr(err error, code ...int) ErrX { 44 if err == nil { 45 return nil 46 } 47 48 c := -1 49 if len(code) > 0 { 50 c = code[0] 51 } 52 return &errX{c, err.Error()} 53 }