-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
170 lines (150 loc) · 3.97 KB
/
errors.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package validator
import "bytes"
type ParamsError struct {
Key string
Value interface{}
Args []interface{}
Text string
}
var (
defaultUnknownParamTpl = "未知的参数:{{.Key}}"
defaultRequireParamTpl = "{{.Key}}是必须的参数"
defaultRequireNotNullTpl = "{{.Key}}是必须的参数,不能为空"
defaultMustLengthTpl = "参数[{{.Key}}]的长度必须为{{index .Args 0}}"
defaultMustMinTpl = "参数[{{.Key}}]的最小值必须大于{{index .Args 0}}"
defaultMustMaxTpl = "参数[{{.Key}}]的最大值必须小于{{index .Args 0}}"
defaultMustLengthRangeTpl = "参数[{{.Key}}]的长度必须为大于{{index .Args 0}}小于{{index .Args 1}}"
defaultMustValuesTpl = `参数[{{.Key}}]的值必须在{{index .Args 0}}的范围中`
defaultMustTimeLayoutTpl = "参数[{{.Key}}]的格式必须是{{index .Args 0}}"
defaultMustLessThanTpl = "参数[{{.Key}}]的值必须小于参数[{{index .Args 0}}]"
defaultMustLargeThanTpl = "参数[{{.Key}}]的值必须大于参数[{{index .Args 0}}]"
)
var (
CustomUnknownParamTpl = "{{.unknow_param}}"
CustomRequireParamTpl = "{{.require_param}}"
CustomRequireNotNullTpl = "{{.require_not_null}}"
CustomMustLengthTpl = "{{.must_length}}"
CustomMustMinTpl = "{{.must_min}}"
CustomMustMaxTpl = "{{.must_max}}"
CustomMustLengthRangeTpl = "{{.must_length_range}}"
CustomMustValuesTpl = "{{.must_values}}"
CustomMustTimeLayoutTpl = "{{.must_time_layout}}"
CustomMustLessThanTpl = "{{.must_less_than}}"
CustomMustLargeThanTpl = "{{.must_large_than}}"
)
//错误接口
func (p *ParamsError) Error() string {
return p.Text
}
//位置参数
func (p *ParamsError) ErrUnknownParam(cus bool) *ParamsError {
if cus {
p.Text = CustomUnknownParamTpl
return p
}
p.Text = defaultUnknownParamTpl
return p.Tr()
}
//用于自定义参数
func (p *ParamsError) CustomErrorText(text string) *ParamsError {
p.Text = text
return p
}
func (p *ParamsError) ErrRequireParam(cus bool) *ParamsError {
if cus {
p.Text = CustomRequireParamTpl
return p
}
p.Text = defaultRequireParamTpl
return p.Tr()
}
func (p *ParamsError) ErrRequireNotNull(cus bool) *ParamsError {
if cus {
p.Text = CustomRequireNotNullTpl
return p
}
p.Text = defaultRequireNotNullTpl
return p.Tr()
}
func (p *ParamsError) ErrMustLength(cus bool) *ParamsError {
if cus {
p.Text = CustomMustLengthTpl
return p
}
p.Text = defaultMustLengthTpl
return p.Tr()
}
func (p *ParamsError) ErrMustMin(cus bool) *ParamsError {
if cus {
p.Text = CustomMustMinTpl
return p
}
p.Text = defaultMustMinTpl
return p.Tr()
}
func (p *ParamsError) ErrMustMax(cus bool) *ParamsError {
if cus {
p.Text = CustomMustMaxTpl
return p
}
p.Text = defaultMustMaxTpl
return p.Tr()
}
func (p *ParamsError) ErrMustLengthRange(cus bool) *ParamsError {
if cus {
p.Text = CustomMustLengthRangeTpl
return p
}
p.Text = defaultMustLengthRangeTpl
return p.Tr()
}
func (p *ParamsError) ErrMustValues(cus bool) *ParamsError {
if cus {
p.Text = CustomMustValuesTpl
return p
}
p.Text = defaultMustValuesTpl
return p.Tr()
}
func (p *ParamsError) ErrMustTimeLayout(cus bool) *ParamsError {
if cus {
p.Text = CustomMustTimeLayoutTpl
return p
}
p.Text = defaultMustTimeLayoutTpl
return p.Tr()
}
func (p *ParamsError) ErrMustLessThan(cus bool) *ParamsError {
if cus {
p.Text = CustomMustLessThanTpl
return p
}
p.Text = defaultMustLessThanTpl
return p.Tr()
}
func (p *ParamsError) ErrMustLargeThan(cus bool) *ParamsError {
if cus {
p.Text = CustomMustLargeThanTpl
return p
}
p.Text = defaultMustLargeThanTpl
return p.Tr()
}
func (p *ParamsError) Tr() *ParamsError {
var buff = make([]byte, 0)
b := bytes.NewBuffer(buff)
tmpl(b, p.Text, p)
p.Text = b.String()
return p
}
func NewParamsError(k string, v interface{}) *ParamsError {
pErr := new(ParamsError)
pErr.Key = k
pErr.Value = v
return pErr
}
func NewTextError(text string) *ParamsError {
pErr := new(ParamsError)
pErr.Text = text
return pErr
}