forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerror.go
159 lines (137 loc) · 3.27 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
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
package tp
import (
"encoding/json"
"strconv"
"unsafe"
"github.com/tidwall/gjson"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/teleport/utils"
)
type (
// Rerror error only for reply message
Rerror struct {
// Code error code
Code int32
// Message the error message displayed to the user (optional)
Message string
// Reason the cause of the error for debugging (optional)
Reason string
}
)
var (
_ json.Marshaler = new(Rerror)
_ json.Unmarshaler = new(Rerror)
reA = []byte(`{"code":`)
reB = []byte(`,"message":`)
reC = []byte(`,"reason":`)
)
// NewRerror creates a *Rerror.
func NewRerror(code int32, message, reason string) *Rerror {
return &Rerror{
Code: code,
Message: message,
Reason: reason,
}
}
// NewRerrorFromMeta creates a *Rerror from 'X-Reply-Error' metadata.
// Return nil if there is no 'X-Reply-Error' in metadata.
func NewRerrorFromMeta(meta *utils.Args) *Rerror {
b := meta.Peek(MetaRerror)
if len(b) == 0 {
return nil
}
r := new(Rerror)
r.UnmarshalJSON(b)
return r
}
// SetToMeta sets self to 'X-Reply-Error' metadata.
func (r *Rerror) SetToMeta(meta *utils.Args) {
b, _ := r.MarshalJSON()
if len(b) == 0 {
return
}
meta.Set(MetaRerror, goutil.BytesToString(b))
}
// Copy returns the copy of Rerror
func (r Rerror) Copy() *Rerror {
return &r
}
// SetMessage sets the error message displayed to the user.
func (r *Rerror) SetMessage(message string) *Rerror {
r.Message = message
return r
}
// SetReason sets the cause of the error for debugging.
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.MarshalJSON()
return goutil.BytesToString(b)
}
// MarshalJSON marshals Rerror into JSON, implements json.Marshaler interface.
func (r *Rerror) MarshalJSON() ([]byte, error) {
if r == nil {
return []byte{}, nil
}
var b = append(reA, strconv.FormatInt(int64(r.Code), 10)...)
if len(r.Message) > 0 {
b = append(b, reB...)
b = append(b, utils.ToJsonStr(goutil.StringToBytes(r.Message), false)...)
}
if len(r.Reason) > 0 {
b = append(b, reC...)
b = append(b, utils.ToJsonStr(goutil.StringToBytes(r.Reason), false)...)
}
b = append(b, '}')
return b, nil
}
// UnmarshalJSON unmarshals a JSON description of self.
func (r *Rerror) UnmarshalJSON(b []byte) error {
if r == nil {
return nil
}
s := goutil.BytesToString(b)
r.Code = int32(gjson.Get(s, "code").Int())
r.Message = gjson.Get(s, "message").String()
r.Reason = gjson.Get(s, "reason").String()
return nil
}
func hasRerror(meta *utils.Args) bool {
return meta.Has(MetaRerror)
}
func getRerrorBytes(meta *utils.Args) []byte {
return meta.Peek(MetaRerror)
}
// ToError converts to error
func (r *Rerror) ToError() error {
if r == nil {
return nil
}
return (*rerror)(unsafe.Pointer(r))
}
// ToRerror converts error to *Rerror
func ToRerror(err error) *Rerror {
if err == nil {
return nil
}
r, ok := err.(*rerror)
if ok {
return r.toRerror()
}
rerr := rerrUnknownError.Copy().SetReason(err.Error())
return rerr
}
type rerror Rerror
func (r *rerror) Error() string {
b, _ := r.toRerror().MarshalJSON()
return goutil.BytesToString(b)
}
func (r *rerror) toRerror() *Rerror {
return (*Rerror)(unsafe.Pointer(r))
}