-
Notifications
You must be signed in to change notification settings - Fork 1
/
rerror.go
61 lines (52 loc) · 1.11 KB
/
rerror.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
package td
import "encoding/json"
type (
// Rerror 发生错误时返回的错误信息 response error
Rerror struct {
// Code 错误状态码
Code int `json:"code"`
// Message 错误信息
Message string `json:"message"`
// Reason 错误原因
Reason string `json:"reason"`
}
)
// NewRerror
func NewRerror(code int, message, reason string) *Rerror {
return &Rerror{
Code: code,
Message: message,
Reason: reason,
}
}
// SetCode
func (r *Rerror) SetCode(message string) *Rerror {
r.Message = message
return r
}
// SetMessage
func (r *Rerror) SetMessage(message string) *Rerror {
r.Message = message
return r
}
// SetReason
func (r *Rerror) SetReason(reason string) *Rerror {
r.Reason = reason
return r
}
// String prints error info.
func (r *Rerror) String() string {
if r == nil {
return "<nil>"
}
b, _ := r.MarshalRerror()
return BytesToString(b)
}
// MarshalRerror 错误信息编码
func (r *Rerror) MarshalRerror() ([]byte, error) {
return json.Marshal(r)
}
// UnmarshalRerror 解析错误信息
func (r *Rerror) UnmarshalRerror(data []byte) error {
return json.Unmarshal(data, &r)
}