Skip to content

Commit cbd40c2

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 da09c28 commit cbd40c2

12 files changed

+536
-15
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

+117-8
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,25 +158,132 @@ func decodeBoxError(d *decoder) (*BoxError, error) {
156158
return &errorStack[0], nil
157159
}
158160

161+
func encodeBoxError(enc *encoder, boxError *BoxError) error {
162+
if boxError == nil {
163+
return fmt.Errorf("msgpack: unexpected nil BoxError on encode")
164+
}
165+
166+
if err := enc.EncodeMapLen(1); err != nil {
167+
return err
168+
}
169+
if err := encodeUint(enc, keyErrorStack); err != nil {
170+
return err
171+
}
172+
173+
var stackDepth = boxError.Depth()
174+
if err := enc.EncodeArrayLen(stackDepth); err != nil {
175+
return err
176+
}
177+
178+
for ; stackDepth > 0; stackDepth-- {
179+
fieldsLen := len(boxError.Fields)
180+
181+
if fieldsLen > 0 {
182+
if err := enc.EncodeMapLen(7); err != nil {
183+
return err
184+
}
185+
} else {
186+
if err := enc.EncodeMapLen(6); err != nil {
187+
return err
188+
}
189+
}
190+
191+
if err := encodeUint(enc, keyErrorType); err != nil {
192+
return err
193+
}
194+
if err := enc.EncodeString(boxError.Type); err != nil {
195+
return err
196+
}
197+
198+
if err := encodeUint(enc, keyErrorFile); err != nil {
199+
return err
200+
}
201+
if err := enc.EncodeString(boxError.File); err != nil {
202+
return err
203+
}
204+
205+
if err := encodeUint(enc, keyErrorLine); err != nil {
206+
return err
207+
}
208+
if err := enc.EncodeUint64(boxError.Line); err != nil {
209+
return err
210+
}
211+
212+
if err := encodeUint(enc, keyErrorMessage); err != nil {
213+
return err
214+
}
215+
if err := enc.EncodeString(boxError.Msg); err != nil {
216+
return err
217+
}
218+
219+
if err := encodeUint(enc, keyErrorErrno); err != nil {
220+
return err
221+
}
222+
if err := enc.EncodeUint64(boxError.Errno); err != nil {
223+
return err
224+
}
225+
226+
if err := encodeUint(enc, keyErrorErrcode); err != nil {
227+
return err
228+
}
229+
if err := enc.EncodeUint64(boxError.Code); err != nil {
230+
return err
231+
}
232+
233+
if fieldsLen > 0 {
234+
if err := encodeUint(enc, keyErrorFields); err != nil {
235+
return err
236+
}
237+
238+
if err := enc.EncodeMapLen(fieldsLen); err != nil {
239+
return err
240+
}
241+
242+
for k, v := range boxError.Fields {
243+
if err := enc.EncodeString(k); err != nil {
244+
return err
245+
}
246+
if err := enc.Encode(v); err != nil {
247+
return err
248+
}
249+
}
250+
}
251+
252+
if stackDepth > 1 {
253+
boxError = boxError.Prev
254+
}
255+
}
256+
257+
return nil
258+
}
259+
159260
// UnmarshalMsgpack deserializes a BoxError value from a MessagePack
160261
// representation.
161262
func (e *BoxError) UnmarshalMsgpack(b []byte) error {
162-
var val *BoxError
163-
var err error
164-
165263
buf := bytes.NewBuffer(b)
166264
dec := newDecoder(buf)
167265

168-
if val, err = decodeBoxError(dec); err != nil {
266+
if val, err := decodeBoxError(dec); err != nil {
169267
return err
170-
}
171-
172-
if val != nil {
268+
} else {
173269
if e == nil {
174270
e = &BoxError{}
175271
}
272+
176273
*e = *val
274+
275+
return nil
177276
}
277+
}
178278

179-
return nil
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 nil, err
286+
}
287+
288+
return buf.Bytes(), nil
180289
}

0 commit comments

Comments
 (0)