-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckerr.go
92 lines (80 loc) · 1.61 KB
/
checkerr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package checkerr
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/gocomb/tools/checkerr/queue"
)
var msgQ = queue.New()
//入口函数
func Check(args ...interface{}) CheckErr {
errCheck := CheckErr{}
errCheck.val = args
return errCheck
}
//根据method执行错误处理方式,以切片形式返回原函数返回值
func (p *CheckErr) Do(method string) []interface{} {
val := p.val.([]interface{})
switch method {
case "print":
printErr(val)
case "painc":
panicErr(val)
case "push":
push(val)
}
return val
}
//打印错误
func printErr(args []interface{}) {
for _, v := range args {
if v != nil {
if strings.Contains(reflect.TypeOf(v).String(), "error") {
fmt.Printf("%c[1;31m%s[ERROR]\t%s%c[0m\n\n", 0x1B, time.Now().Format("2006/01/02 - 15:04:05"), v, 0x1B)
}
}
}
}
//painc错误
func panicErr(args []interface{}) {
for _, v := range args {
if v != nil {
if strings.Contains(reflect.TypeOf(v).String(), "error") {
panic(v)
}
}
}
}
//加入error队列
func push(args []interface{}) {
for _, v := range args {
if v != nil {
if strings.Contains(reflect.TypeOf(v).String(), "error") {
err := v.(error)
msgQ.Push(err)
}
}
}
}
//从队列中取出error
func GetErr() error {
g := msgQ.GetErrMQ()
return g.FetchErr()
}
//自定义方法
func (p *CheckErr) Def(arg func(...interface{})) (func(...interface{})) {
temp := (p.val).([]interface{})
for _, val := range temp {
if val != nil {
if strings.Contains(reflect.TypeOf(val).String(), "error") {
return arg
}
}
}
return voidFun
}
//空函数
func voidFun(void ...interface{}) {
void = nil
}