forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
/
box_error.go
305 lines (260 loc) · 6.57 KB
/
box_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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package tarantool
import (
"bytes"
"fmt"
"github.com/vmihailenco/msgpack/v5"
)
const errorExtID = 3
const (
keyErrorStack = 0x00
keyErrorType = 0x00
keyErrorFile = 0x01
keyErrorLine = 0x02
keyErrorMessage = 0x03
keyErrorErrno = 0x04
keyErrorErrcode = 0x05
keyErrorFields = 0x06
)
// BoxError is a type representing Tarantool `box.error` object: a single
// MP_ERROR_STACK object with a link to the previous stack error.
// See https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error/
//
// Since 1.10.0
type BoxError struct {
// Type is error type that implies its source (for example, "ClientError").
Type string
// File is a source code file where the error was caught.
File string
// Line is a number of line in the source code file where the error was caught.
Line uint64
// Msg is the text of reason.
Msg string
// Errno is the ordinal number of the error.
Errno uint64
// Code is the number of the error as defined in `errcode.h`.
Code uint64
// Fields are additional fields depending on error type. For example, if
// type is "AccessDeniedError", then it will include "object_type",
// "object_name", "access_type".
Fields map[string]interface{}
// Prev is the previous error in stack.
Prev *BoxError
}
// Error converts a BoxError to a string.
func (e *BoxError) Error() string {
s := fmt.Sprintf("%s (%s, code 0x%x), see %s line %d",
e.Msg, e.Type, e.Code, e.File, e.Line)
if e.Prev != nil {
return fmt.Sprintf("%s: %s", s, e.Prev)
}
return s
}
// Depth computes the count of errors in stack, including the current one.
func (e *BoxError) Depth() int {
depth := int(0)
cur := e
for cur != nil {
cur = cur.Prev
depth++
}
return depth
}
func decodeBoxError(d *msgpack.Decoder) (*BoxError, error) {
var l, larr, l1, l2 int
var errorStack []BoxError
var err error
if l, err = d.DecodeMapLen(); err != nil {
return nil, err
}
for ; l > 0; l-- {
var cd int
if cd, err = d.DecodeInt(); err != nil {
return nil, err
}
switch cd {
case keyErrorStack:
if larr, err = d.DecodeArrayLen(); err != nil {
return nil, err
}
errorStack = make([]BoxError, larr)
for i := 0; i < larr; i++ {
if l1, err = d.DecodeMapLen(); err != nil {
return nil, err
}
for ; l1 > 0; l1-- {
var cd1 int
if cd1, err = d.DecodeInt(); err != nil {
return nil, err
}
switch cd1 {
case keyErrorType:
if errorStack[i].Type, err = d.DecodeString(); err != nil {
return nil, err
}
case keyErrorFile:
if errorStack[i].File, err = d.DecodeString(); err != nil {
return nil, err
}
case keyErrorLine:
if errorStack[i].Line, err = d.DecodeUint64(); err != nil {
return nil, err
}
case keyErrorMessage:
if errorStack[i].Msg, err = d.DecodeString(); err != nil {
return nil, err
}
case keyErrorErrno:
if errorStack[i].Errno, err = d.DecodeUint64(); err != nil {
return nil, err
}
case keyErrorErrcode:
if errorStack[i].Code, err = d.DecodeUint64(); err != nil {
return nil, err
}
case keyErrorFields:
var mapk string
var mapv interface{}
errorStack[i].Fields = make(map[string]interface{})
if l2, err = d.DecodeMapLen(); err != nil {
return nil, err
}
for ; l2 > 0; l2-- {
if mapk, err = d.DecodeString(); err != nil {
return nil, err
}
if mapv, err = d.DecodeInterface(); err != nil {
return nil, err
}
errorStack[i].Fields[mapk] = mapv
}
default:
if err = d.Skip(); err != nil {
return nil, err
}
}
}
if i > 0 {
errorStack[i-1].Prev = &errorStack[i]
}
}
default:
if err = d.Skip(); err != nil {
return nil, err
}
}
}
if len(errorStack) == 0 {
return nil, fmt.Errorf("msgpack: unexpected empty BoxError stack on decode")
}
return &errorStack[0], nil
}
func encodeBoxError(enc *msgpack.Encoder, boxError *BoxError) error {
if boxError == nil {
return fmt.Errorf("msgpack: unexpected nil BoxError on encode")
}
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorStack); err != nil {
return err
}
var stackDepth = boxError.Depth()
if err := enc.EncodeArrayLen(stackDepth); err != nil {
return err
}
for ; stackDepth > 0; stackDepth-- {
fieldsLen := len(boxError.Fields)
if fieldsLen > 0 {
if err := enc.EncodeMapLen(7); err != nil {
return err
}
} else {
if err := enc.EncodeMapLen(6); err != nil {
return err
}
}
if err := enc.EncodeUint(keyErrorType); err != nil {
return err
}
if err := enc.EncodeString(boxError.Type); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorFile); err != nil {
return err
}
if err := enc.EncodeString(boxError.File); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorLine); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Line); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorMessage); err != nil {
return err
}
if err := enc.EncodeString(boxError.Msg); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorErrno); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Errno); err != nil {
return err
}
if err := enc.EncodeUint(keyErrorErrcode); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Code); err != nil {
return err
}
if fieldsLen > 0 {
if err := enc.EncodeUint(keyErrorFields); err != nil {
return err
}
if err := enc.EncodeMapLen(fieldsLen); err != nil {
return err
}
for k, v := range boxError.Fields {
if err := enc.EncodeString(k); err != nil {
return err
}
if err := enc.Encode(v); err != nil {
return err
}
}
}
if stackDepth > 1 {
boxError = boxError.Prev
}
}
return nil
}
// UnmarshalMsgpack deserializes a BoxError value from a MessagePack
// representation.
func (e *BoxError) UnmarshalMsgpack(b []byte) error {
if e == nil {
panic("cannot unmarshal to a nil pointer")
}
buf := bytes.NewBuffer(b)
dec := msgpack.NewDecoder(buf)
if val, err := decodeBoxError(dec); err != nil {
return err
} else {
*e = *val
return nil
}
}
// MarshalMsgpack serializes the BoxError into a MessagePack representation.
func (e *BoxError) MarshalMsgpack() ([]byte, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
if err := encodeBoxError(enc, e); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func init() {
msgpack.RegisterExt(errorExtID, (*BoxError)(nil))
}