-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
62 lines (52 loc) · 1.05 KB
/
error.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
package mago
import "strings"
// Error 普通错误
type Error interface {
Code() ErrorCode
HttpCode() int
Msg() string
WithMsg(string) Error
SetTemplate(name string, value string) Error
Error() string
}
type errorContext struct {
code ErrorCode
httpCode int
msg string
template map[string]string
}
func NewError(code ErrorCode) Error {
ins := &errorContext{}
ins.code = code
ins.httpCode = code.HttpCode()
ins.msg = code.String()
ins.template = make(map[string]string)
return ins
}
func (e *errorContext) Code() ErrorCode {
return e.code
}
func (e *errorContext) HttpCode() int {
return e.httpCode
}
func (e *errorContext) WithMsg(msg string) Error {
e.msg = msg
return e
}
func (e *errorContext) SetTemplate(name string, value string) Error {
e.template[name] = value
return e
}
func (e *errorContext) Msg() string {
if len(e.template) > 0 {
newMsg := ""
for k, v := range e.template {
newMsg = strings.ReplaceAll(e.msg, k, v)
}
return newMsg
}
return e.msg
}
func (e *errorContext) Error() string {
return e.msg
}