-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcode.go
144 lines (128 loc) · 3.04 KB
/
xcode.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package xcode
import (
"fmt"
"github.com/coder2z/g-saber/xjson"
"github.com/coder2z/g-saber/xlog"
"net/http"
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/golang/protobuf/ptypes/any"
spb "google.golang.org/genproto/googleapis/rpc/status"
)
// CodeBreakUp 低于10000均为系统错误码,业务错误码请使用10000以上
const (
CodeBreakUp uint32 = 9999
SystemType = iota
BusinessType
)
var (
aid uint32
_codesSystem sync.Map
_codesBusiness sync.Map
// OK ...
OK = add(SystemType, uint32(codes.OK), "OK")
Unknown = add(SystemType, uint32(codes.Unknown), "UNKNOWN")
)
type CodeInfo struct {
CodeT uint
Code uint32
Message string
}
func XCodeSystemCodeHttp(w http.ResponseWriter, r *http.Request) {
var res = make([]*spbStatus, 0)
_codesSystem.Range(func(key, val interface{}) bool {
res = append(res, val.(*spbStatus))
return true
})
_ = xjson.NewEncoder(w).Encode(res)
}
func XCodeBusinessCodeHttp(w http.ResponseWriter, r *http.Request) {
var res = make([]*spbStatus, 0)
_codesBusiness.Range(func(key, val interface{}) bool {
res = append(res, val.(*spbStatus))
return true
})
_ = xjson.NewEncoder(w).Encode(res)
}
func SystemCode(code uint32) (spb *spbStatus) {
_codesSystem.Range(func(key, val interface{}) bool {
if code == key.(uint32) {
spb = val.(*spbStatus)
return false
}
return true
})
if spb == nil {
spb = Unknown
}
return spb
}
func BusinessCode(code uint32) (spb *spbStatus) {
_codesBusiness.Range(func(key, val interface{}) bool {
if code == key.(uint32) {
spb = val.(*spbStatus)
return false
}
return true
})
if spb == nil {
spb = Unknown
}
return spb
}
func SystemCodeAdd(code uint32, message string) *spbStatus {
if code > CodeBreakUp {
xlog.Panic("Application System Panic",
xlog.FieldErr(fmt.Errorf("customize code must less than 9999")),
xlog.FieldComponentName("XCode"),
xlog.FieldMethod("XCode.SystemCodeAdd"),
)
}
return add(SystemType, aid*10000+code, message)
}
func BusinessCodeAdd(code uint32, message string) *spbStatus {
if code < CodeBreakUp {
xlog.Panic("Application System Panic",
xlog.FieldErr(fmt.Errorf("customize code must less than 9999")),
xlog.FieldComponentName("XCode"),
xlog.FieldMethod("XCode.BusinessCodeAdd"),
)
}
return add(BusinessType, code, message)
}
func CodeAdds(data []CodeInfo) {
for _, datum := range data {
_ = add(datum.CodeT, datum.Code, datum.Message)
}
}
func add(codeT uint, code uint32, message string) *spbStatus {
s := &spbStatus{
&spb.Status{
Code: int32(code),
Message: message,
Details: make([]*any.Any, 0),
},
}
if codeT == SystemType {
_codesSystem.Store(code, s)
}
if codeT == BusinessType {
_codesBusiness.Store(code, s)
}
return s
}
// ExtractCodes cause from error to ecode.
func ExtractCodes(e error) *spbStatus {
if e == nil {
return OK
}
gst, _ := status.FromError(e)
return &spbStatus{
&spb.Status{
Code: int32(gst.Code()),
Message: gst.Message(),
Details: make([]*any.Any, 0),
},
}
}