-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
129 lines (108 loc) · 2.61 KB
/
Copy patherror.go
File metadata and controls
129 lines (108 loc) · 2.61 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
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
package xerror
import (
"encoding/json"
"fmt"
"github.com/save95/xerror/ecode"
"github.com/save95/xerror/xcode"
)
type xError struct {
code xcode.XCode
error error
fields []interface{}
}
func (e *xError) String() string {
if e.code == nil {
return xcode.InternalServerError.String()
}
return e.code.String()
}
func (e *xError) Error() string {
if e.error == nil {
return xcode.InternalServerError.String()
}
return e.error.Error()
}
func (e *xError) Unwrap() error {
return e.error
}
func (e *xError) HttpStatus() int {
if e.code == nil {
return xcode.InternalServerError.HttpStatus()
}
return e.code.HttpStatus()
}
func (e *xError) ErrorCode() int {
if e.code == nil {
return xcode.InternalServerError.Code()
}
return e.code.Code()
}
func (e *xError) ToMessage(config *ecode.Config) string {
var defaultMsg, clientMsg string
// 从资源仓库获取所有端口的解析错误码解析规则
if config != nil && config.Repository != nil {
if es, err := config.Repository.FindByCode(e.code.Code()); nil == err {
for _, entity := range es {
if entity.GetCode() != e.code.Code() {
break
}
// 取得默认消息
if entity.GetClient() == int(ecode.ClientDefault) {
defaultMsg = entity.GetMessage()
}
if entity.GetClient() == int(config.Client) && len(entity.GetMessage()) > 0 {
clientMsg = entity.GetMessage()
break
}
}
}
}
// 如果找到对应端口消息,则返回端口消息
// 否则,查看是否有端口默认消息,如果有,则返回
// 否则,查看是否有 XCode 消息,如果有,则返回
// 否则,返回错误消息
if len(clientMsg) > 0 {
return clientMsg
} else if len(defaultMsg) > 0 {
return defaultMsg
} else if len(e.code.String()) > 0 {
return e.code.String()
} else {
return e.Error()
}
}
func (e *xError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
// 展示 xField 内容
if e.fields != nil && len(e.fields) > 0 {
if bs, err := json.Marshal(e.fields); nil == err {
_, _ = fmt.Fprintf(s, "\nfields: %s\n", string(bs))
}
}
_, _ = fmt.Fprintf(s, "%+v", e.error)
return
}
//fallthrough
//case 's':
// if s.Flag('-') {
// _, _ = io.WriteString(s, e.String())
// return
// }
// _, _ = fmt.Fprintf(s, "[%d] %s", e.ErrorCode(), e.error)
}
}
func (e *xError) WithFields(field ...interface{}) *xError {
if e.fields == nil {
e.fields = make([]interface{}, 0)
}
e.fields = append(e.fields, field...)
return e
}
func (e *xError) GetFields() []interface{} {
if e.fields == nil {
return make([]interface{}, 0)
}
return e.fields
}