Skip to content

Commit

Permalink
api: support error type in MessagePack
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DifferentialOrange committed Dec 1, 2022
1 parent 6ff1c75 commit 87fe913
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

- Support iproto feature discovery (#120).
- Support errors extended information (#209).
- Error type support in MessagePack (#209).

### Changed

Expand Down
109 changes: 109 additions & 0 deletions box_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
)

const errorExtID = 3

// 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/
Expand Down Expand Up @@ -156,6 +158,101 @@ func decodeBoxError(d *decoder) (*BoxError, error) {
return nil, nil
}

func encodeBoxError(enc *encoder, boxError *BoxError) error {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := encodeUint(enc, 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 := encodeUint(enc, keyErrorType); err != nil {
return err
}
if err := enc.EncodeString(boxError.Type); err != nil {
return err
}

if err := encodeUint(enc, keyErrorFile); err != nil {
return err
}
if err := enc.EncodeString(boxError.File); err != nil {
return err
}

if err := encodeUint(enc, keyErrorLine); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Line); err != nil {
return err
}

if err := encodeUint(enc, keyErrorMessage); err != nil {
return err
}
if err := enc.EncodeString(boxError.Msg); err != nil {
return err
}

if err := encodeUint(enc, keyErrorErrno); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Errno); err != nil {
return err
}

if err := encodeUint(enc, keyErrorErrcode); err != nil {
return err
}
if err := enc.EncodeUint64(boxError.Code); err != nil {
return err
}

if fieldsLen > 0 {
if err := encodeUint(enc, 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 {
Expand All @@ -178,3 +275,15 @@ func (e *BoxError) UnmarshalMsgpack(b []byte) error {

return nil
}

// MarshalMsgpack serializes the BoxError into a MessagePack representation.
func (e *BoxError) MarshalMsgpack() ([]byte, error) {
var buf bytes.Buffer

enc := newEncoder(&buf)
if err := encodeBoxError(enc, e); err != nil {
return []byte{}, err
}

return buf.Bytes(), nil
}
Loading

0 comments on commit 87fe913

Please sign in to comment.