diff --git a/init.go b/init.go index 88e5cdd..524caf5 100644 --- a/init.go +++ b/init.go @@ -1,10 +1,5 @@ package xerror -// func caller depth -const ( - callDepth = 3 -) - var ( // ErrDone done ErrDone = New("DONE") diff --git a/util.go b/util.go index d2c1897..2c6c9c6 100644 --- a/util.go +++ b/util.go @@ -20,7 +20,7 @@ func handleErr(err *error, _err interface{}) { case string: *err = errors.New(_err) default: - *err = New(ErrUnknownType.Code, fmt.Sprintf("%+v", _err)) + *err = WrapF(ErrUnknownType, "%+v", _err) } } @@ -31,7 +31,7 @@ func handle(err error, msg string, args ...interface{}) *xerror { err2 := &xerror{} err2.Msg = msg - err2.Caller = callerWithDepth(callDepth + 1) + err2.Caller = callerWithDepth(xerror_core.CallDepth + 1) err2.Cause1 = err return err2 } @@ -45,7 +45,7 @@ func callerWithDepth(callDepths ...int) string { return "" } - var cd = callDepth + var cd = xerror_core.CallDepth if len(callDepths) > 0 { cd = callDepths[0] } @@ -58,7 +58,7 @@ func callerWithDepth(callDepths ...int) string { f := frame(pcs[0]) fn := runtime.FuncForPC(f.pc()) if fn == nil { - return "unknown type" + return ErrUnknownType.Error() } file, line := fn.FileLine(f.pc()) @@ -67,7 +67,7 @@ func callerWithDepth(callDepths ...int) string { func callerWithFunc(fn reflect.Value) string { if !fn.IsValid() || fn.IsNil() || fn.Kind() != reflect.Func { - panic(ErrNotFuncType) + Panic(ErrNotFuncType) } var _fn = fn.Pointer() var file, line = runtime.FuncForPC(_fn).FileLine(_fn) diff --git a/xerror.go b/xerror.go index d4edd81..451aa2c 100644 --- a/xerror.go +++ b/xerror.go @@ -2,6 +2,7 @@ package xerror import ( "fmt" + "github.com/pubgo/xerror/xerror_core" "net/http" "os" "reflect" @@ -28,7 +29,7 @@ func New(code string, ms ...string) *xerrorBase { xw := &xerrorBase{} xw.Code = code xw.Msg = msg - xw.Caller = callerWithDepth(callDepth) + xw.Caller = callerWithDepth(xerror_core.CallDepth) return xw } @@ -58,7 +59,7 @@ func Resp(f func(err XErr)) { f(err.(XErr)) return } - f(&xerror{Cause1: err, Caller: callerWithDepth(callDepth + 1)}) + f(&xerror{Cause1: err, Caller: callerWithDepth(xerror_core.CallDepth + 1)}) } func RespExit() { diff --git a/xerror_color.go b/xerror_color.go index d298593..0086328 100644 --- a/xerror_color.go +++ b/xerror_color.go @@ -2,8 +2,11 @@ package xerror import "fmt" +// Color represents a text color. +type color uint8 + const ( - colorBlack = iota + 30 + colorBlack color = iota + 30 colorRed colorGreen colorYellow @@ -11,11 +14,11 @@ const ( colorMagenta colorCyan colorWhite - - colorBold = 1 - colorDarkGray = 90 + colorBold color = 1 + colorDarkGray color = 90 ) -func colorize(s interface{}, c int) string { - return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s) +// Add adds the coloring to the given string. +func (c color) P(s string, args ...interface{}) string { + return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), fmt.Sprintf(s, args...)) } diff --git a/xerror_core/core.go b/xerror_core/core.go index 80b6e80..7f0d5cd 100644 --- a/xerror_core/core.go +++ b/xerror_core/core.go @@ -1,6 +1,7 @@ package xerror_core var IsCaller bool +var CallDepth = 3 func init() { IsCaller = true diff --git a/xrr.go b/xrr.go index c2a49cd..be57065 100644 --- a/xrr.go +++ b/xrr.go @@ -3,12 +3,13 @@ package xerror import ( "encoding/json" "fmt" + "github.com/pubgo/xerror/xerror_core" "io" "strings" ) type xerrorBase struct { - Code string `json:"code,omitempty"` + Code string `json:"code,omitempty"` Msg string `json:"msg,omitempty"` Caller string `json:"caller,omitempty"` } @@ -27,7 +28,7 @@ func (t *xerrorBase) New(code string, ms ...string) error { xw := &xerrorBase{} xw.Code = code xw.Msg = msg - xw.Caller = callerWithDepth(callDepth) + xw.Caller = callerWithDepth(xerror_core.CallDepth) return xw } @@ -72,15 +73,15 @@ func (t *xerror) p() string { for xrr != nil { buf.WriteString("========================================================================================================================\n") if xrr.Cause1 != nil { - buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorize("Err", colorRed), xrr.Cause1)) + buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorRed.P("Err"), xrr.Cause1)) } if xrr.Msg != "" { - buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorize("Msg", colorGreen), xrr.Msg)) + buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorGreen.P("Msg"), xrr.Msg)) } if xrr.Code1 != "" { - buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorize("Code", colorGreen), xrr.Code1)) + buf.WriteString(fmt.Sprintf(" %s]: %s\n", colorGreen.P("Code"), xrr.Code1)) } - buf.WriteString(fmt.Sprintf("%s]: %s\n", colorize("Caller", colorYellow), xrr.Caller)) + buf.WriteString(fmt.Sprintf("%s]: %s\n", colorYellow.P("Caller"), xrr.Caller)) xrr = trans(xrr.Cause1) } buf.WriteString("========================================================================================================================\n\n") @@ -93,6 +94,8 @@ func (t *xerror) Is(err error) bool { } switch err := err.(type) { + case *xerrorBase: + return err == t.Cause1 || err.Code == t.Code1 case *xerror: return err == t || err.Cause1 == t.Cause1 || err.Code1 == t.Code1 case error: @@ -108,6 +111,8 @@ func (t *xerror) As(err interface{}) bool { } switch e := err.(type) { + case *xerrorBase: + return strings.HasPrefix(t.Code1, e.Code) case *xerror: return strings.HasPrefix(t.Code1, e.Code1) case error: