-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassert.go
More file actions
92 lines (83 loc) · 1.46 KB
/
assert.go
File metadata and controls
92 lines (83 loc) · 1.46 KB
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 errors
import (
"fmt"
"reflect"
)
type pcErr interface {
GetBizStatusCode() int32 // 获取业务的状态码
GetBizStatusMessage() string // 获取业务的状态信息
}
type TraceErr interface {
GetErrorCode() int // 获取框架的错误码
}
func OK(ok bool, err *Cause) {
if ok {
return
}
if err != nil {
panic(err) //重新生成调用栈
}
panic(NewCause(1, DefaultCode, "not ok"))
}
func NilErr(err error) {
if IsNil(err) {
return
}
e, ok := err.(*Cause)
if !ok {
e = CloneAs(err, 1)
}
panic(e)
}
func Nil(obj interface{}, err *Cause) {
if IsNil(obj) {
return
}
if err != nil {
panic(err) //重新生成调用栈
}
panic(NewCause(1, DefaultCode, "not nil"))
}
func Nilf(obj interface{}, code int, format string, a ...interface{}) {
if IsNil(obj) {
return
}
panic(NewCause(1, code, fmt.Sprintf(format, a...)))
}
func IsNil(object interface{}) bool {
if object == nil {
return true
}
val := reflect.ValueOf(object)
switch val.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer,
reflect.Slice, reflect.Interface:
return val.IsNil()
default:
return false
}
}
func TryByFunc(fCatch func(interface{}) bool) {
e := recover()
if e == nil {
return
}
if fCatch != nil && fCatch(e) {
return
}
panic(e)
}
func TryErr(perr *error) {
e := recover()
if e == nil {
return
}
if perr == nil {
panic(e)
}
ok := true
if *perr, ok = e.(*Cause); ok {
return
}
panic(e)
}