Skip to content

Commit 08ff8fe

Browse files
api: support error type in MessagePack
Tarantool supports error extension type since version 2.4.1 [1], encoding was introduced in Tarantool 2.10.0 [2]. This patch introduces the support of Tarantool error extension type in msgpack decoders and encoders. Tarantool error extension type objects are decoded to `*tarantool.BoxError` type. `*tarantool.BoxError` may be encoded to Tarantool error extension type objects. Error extension type internals are the same as errors extended information: the only difference is that extra information is encoded as a separate error dictionary field and error extension type objects are encoded as MessagePack extension type objects. The only way to receive an error extension type object from Tarantool is to receive an explicitly built `box.error` object: either from `return box.error.new(...)` or a tuple with it. All errors raised within Tarantool (including those raised with `box.error(...)`) are encoded based on the same rules as simple errors due to backward compatibility. It is possible to create error extension type objects with Go code, but it not likely to be really useful since most of their fields is computed on error initialization on the server side (even for custom error types). This patch also adds ErrorExtensionFeature flag to client protocol features list. Without this flag, all `box.error` object sent over iproto are encoded to string. We behave like Tarantool `net.box` here: if we support the feature, we provide the feature flag. Since it may become too complicated to enable/disable feature flag through import, error extension type is available as a part of the base package, in contrary to Decimal, UUID, Datetime and Interval types which are enabled by importing underscore subpackage. 1. tarantool/tarantool#4398 2. tarantool/tarantool#6433 Closes #209
1 parent ea28628 commit 08ff8fe

12 files changed

+523
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1212

1313
- Support iproto feature discovery (#120).
1414
- Support errors extended information (#209).
15+
- Error type support in MessagePack (#209).
1516

1617
### Changed
1718

box_error.go

+109
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
)
77

8+
const errorExtID = 3
9+
810
// BoxError is a type representing Tarantool `box.error` object: a single
911
// MP_ERROR_STACK object with a link to the previous stack error.
1012
// See https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error/
@@ -156,6 +158,101 @@ func decodeBoxError(d *decoder) (*BoxError, error) {
156158
return nil, nil
157159
}
158160

161+
func encodeBoxError(enc *encoder, boxError *BoxError) error {
162+
if err := enc.EncodeMapLen(1); err != nil {
163+
return err
164+
}
165+
if err := encodeUint(enc, KeyErrorStack); err != nil {
166+
return err
167+
}
168+
169+
var stackDepth = boxError.Depth()
170+
if err := enc.EncodeArrayLen(stackDepth); err != nil {
171+
return err
172+
}
173+
174+
for ; stackDepth > 0; stackDepth-- {
175+
fieldsLen := len(boxError.Fields)
176+
177+
if fieldsLen > 0 {
178+
if err := enc.EncodeMapLen(7); err != nil {
179+
return err
180+
}
181+
} else {
182+
if err := enc.EncodeMapLen(6); err != nil {
183+
return err
184+
}
185+
}
186+
187+
if err := encodeUint(enc, KeyErrorType); err != nil {
188+
return err
189+
}
190+
if err := enc.EncodeString(boxError.Type); err != nil {
191+
return err
192+
}
193+
194+
if err := encodeUint(enc, KeyErrorFile); err != nil {
195+
return err
196+
}
197+
if err := enc.EncodeString(boxError.File); err != nil {
198+
return err
199+
}
200+
201+
if err := encodeUint(enc, KeyErrorLine); err != nil {
202+
return err
203+
}
204+
if err := enc.EncodeUint64(boxError.Line); err != nil {
205+
return err
206+
}
207+
208+
if err := encodeUint(enc, KeyErrorMessage); err != nil {
209+
return err
210+
}
211+
if err := enc.EncodeString(boxError.Msg); err != nil {
212+
return err
213+
}
214+
215+
if err := encodeUint(enc, KeyErrorErrno); err != nil {
216+
return err
217+
}
218+
if err := enc.EncodeUint64(boxError.Errno); err != nil {
219+
return err
220+
}
221+
222+
if err := encodeUint(enc, KeyErrorErrcode); err != nil {
223+
return err
224+
}
225+
if err := enc.EncodeUint64(boxError.Code); err != nil {
226+
return err
227+
}
228+
229+
if fieldsLen > 0 {
230+
if err := encodeUint(enc, KeyErrorFields); err != nil {
231+
return err
232+
}
233+
234+
if err := enc.EncodeMapLen(fieldsLen); err != nil {
235+
return err
236+
}
237+
238+
for k, v := range boxError.Fields {
239+
if err := enc.EncodeString(k); err != nil {
240+
return err
241+
}
242+
if err := enc.Encode(v); err != nil {
243+
return err
244+
}
245+
}
246+
}
247+
248+
if stackDepth > 1 {
249+
boxError = boxError.Prev
250+
}
251+
}
252+
253+
return nil
254+
}
255+
159256
// UnmarshalMsgpack deserializes a BoxError value from a MessagePack
160257
// representation.
161258
func (e *BoxError) UnmarshalMsgpack(b []byte) error {
@@ -178,3 +275,15 @@ func (e *BoxError) UnmarshalMsgpack(b []byte) error {
178275

179276
return nil
180277
}
278+
279+
// MarshalMsgpack serializes the BoxError into a MessagePack representation.
280+
func (e *BoxError) MarshalMsgpack() ([]byte, error) {
281+
var buf bytes.Buffer
282+
283+
enc := newEncoder(&buf)
284+
if err := encodeBoxError(enc, e); err != nil {
285+
return []byte{}, err
286+
}
287+
288+
return buf.Bytes(), nil
289+
}

0 commit comments

Comments
 (0)