Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
fix: 添加color
Browse files Browse the repository at this point in the history
  • Loading branch information
白云辉 committed Jul 31, 2020
1 parent 9ced77b commit cb99f79
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
5 changes: 0 additions & 5 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package xerror

// func caller depth
const (
callDepth = 3
)

var (
// ErrDone done
ErrDone = New("DONE")
Expand Down
10 changes: 5 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
Expand All @@ -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]
}
Expand All @@ -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())
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions xerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xerror

import (
"fmt"
"github.com/pubgo/xerror/xerror_core"
"net/http"
"os"
"reflect"
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 9 additions & 6 deletions xerror_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package xerror

import "fmt"

// Color represents a text color.
type color uint8

const (
colorBlack = iota + 30
colorBlack color = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
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...))
}
1 change: 1 addition & 0 deletions xerror_core/core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xerror_core

var IsCaller bool
var CallDepth = 3

func init() {
IsCaller = true
Expand Down
17 changes: 11 additions & 6 deletions xrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit cb99f79

Please sign in to comment.