Skip to content

Commit a49bcd3

Browse files
it works
1 parent 87fedf3 commit a49bcd3

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

box_error.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ func decodeBoxError(d *decoder) (*BoxError, error) {
129129
}
130130

131131
func msgpackDecodeBoxError(d *decoder, v reflect.Value) (err error) {
132-
boxError, err := decodeBoxError(d)
132+
boxErrorPtr, err := decodeBoxError(d)
133133
if err != nil {
134134
return err
135135
}
136136

137-
v.Set(reflect.ValueOf(boxError))
137+
v.Set(reflect.ValueOf(*boxErrorPtr))
138138
return nil
139139
}
140140

@@ -152,9 +152,9 @@ func encodeBoxError(enc *encoder, boxError BoxError) (err error) {
152152
}
153153

154154
for ; stackDepth > 0; stackDepth-- {
155-
isWithFields := (len(boxError.Fields) > 0)
155+
fieldsLen := len(boxError.Fields)
156156

157-
if isWithFields {
157+
if fieldsLen > 0 {
158158
if err = enc.EncodeMapLen(7); err != nil {
159159
return err
160160
}
@@ -185,13 +185,6 @@ func encodeBoxError(enc *encoder, boxError BoxError) (err error) {
185185
return err
186186
}
187187

188-
if err = encodeUint(enc, KeyErrorFile); err != nil {
189-
return err
190-
}
191-
if err = enc.EncodeString(boxError.File); err != nil {
192-
return err
193-
}
194-
195188
if err = encodeUint(enc, KeyErrorMessage); err != nil {
196189
return err
197190
}
@@ -213,11 +206,15 @@ func encodeBoxError(enc *encoder, boxError BoxError) (err error) {
213206
return err
214207
}
215208

216-
if isWithFields {
209+
if fieldsLen > 0 {
217210
if err = encodeUint(enc, KeyErrorFields); err != nil {
218211
return err
219212
}
220213

214+
if err = enc.EncodeMapLen(fieldsLen); err != nil {
215+
return err
216+
}
217+
221218
for k, v := range boxError.Fields {
222219
if err = enc.Encode(k); err != nil {
223220
return err
@@ -227,6 +224,10 @@ func encodeBoxError(enc *encoder, boxError BoxError) (err error) {
227224
}
228225
}
229226
}
227+
228+
if stackDepth > 1 {
229+
boxError = *boxError.Prev
230+
}
230231
}
231232

232233
return nil

error_ext_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tarantool_test
22

33
import (
4+
"fmt"
5+
"reflect"
46
"testing"
57

68
"github.com/stretchr/testify/require"
@@ -17,6 +19,38 @@ func skipIfErrorExtUnsupported(t *testing.T) {
1719
}
1820
}
1921

22+
type TupleError struct {
23+
val BoxError
24+
}
25+
26+
func (t *TupleError) EncodeMsgpack(e *encoder) error {
27+
if err := e.EncodeArrayLen(1); err != nil {
28+
return err
29+
}
30+
return e.EncodeValue(reflect.ValueOf(&t.val))
31+
}
32+
33+
func (t *TupleError) DecodeMsgpack(d *decoder) error {
34+
var err error
35+
var l int
36+
if l, err = d.DecodeArrayLen(); err != nil {
37+
return err
38+
}
39+
if l != 1 {
40+
return fmt.Errorf("Array length doesn't match: %d", l)
41+
}
42+
43+
res, err := d.DecodeInterface()
44+
if err != nil {
45+
return err
46+
}
47+
var ok bool
48+
if t.val, ok = toBoxError(res); !ok {
49+
return fmt.Errorf("BoxError doesn't match")
50+
}
51+
return nil
52+
}
53+
2054
var samples = []struct {
2155
goErr BoxError
2256
mpBuf string
@@ -87,14 +121,15 @@ func TestErrorExtMPEncodeDecode(t *testing.T) {
87121
t.Run(testcase.ttErrName, func(t *testing.T) {
88122
var buf []byte
89123
var err error
90-
if buf, err = marshal(testcase.goErr); err != nil {
124+
tuple := TupleError{val: testcase.goErr}
125+
if buf, err = marshal(&tuple); err != nil {
91126
t.Fatalf("Failed to encode error ext type '%v' to a MessagePack buffer: %s", testcase.goErr, err)
92127
}
93-
var v BoxError
128+
var v TupleError
94129
if err = unmarshal(buf, &v); err != nil {
95130
t.Fatalf("Failed to decode MessagePack buffer '%x' to a error ext type: %s", buf, err)
96131
}
97-
require.Equal(t, v, testcase.goErr)
132+
require.Equal(t, v.val, testcase.goErr)
98133
})
99134
}
100135
}

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func ExampleProtocolVersion() {
329329
fmt.Println("Connector client protocol features:", clientProtocolInfo.Features)
330330
// Output:
331331
// Connector client protocol version: 4
332-
// Connector client protocol features: [StreamsFeature TransactionsFeature]
332+
// Connector client protocol features: [StreamsFeature TransactionsFeature ErrorExtensionFeature]
333333
}
334334

335335
func ExampleCommitRequest() {

tarantool_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,7 +2868,7 @@ func TestConnectionProtocolInfoSupported(t *testing.T) {
28682868
require.Equal(t,
28692869
clientProtocolInfo,
28702870
ProtocolInfo{
2871-
Version: ProtocolVersion(4),
2871+
Version: ProtocolVersion(4),
28722872
Features: []ProtocolFeature{
28732873
StreamsFeature,
28742874
TransactionsFeature,
@@ -3222,7 +3222,7 @@ func TestExtraErrorInfoFields(t *testing.T) {
32223222
defer conn.Close()
32233223

32243224
// "test" user cannot create functions
3225-
_, err := conn.Eval("box.schema.func.create('forbidden_function')", []interface{}{})
3225+
_, err := conn.Eval("error(access_denied_error)", []interface{}{})
32263226
require.NotNilf(t, err, "expected error on forbidden action")
32273227

32283228
terr, ok := err.(Error)
@@ -3235,15 +3235,15 @@ func TestExtraErrorInfoFields(t *testing.T) {
32353235
require.Greaterf(t, terr.ExtraInfo.Line, int64(0), "line info not empty")
32363236
require.Equal(t,
32373237
terr.ExtraInfo.Message,
3238-
"Create access to function 'forbidden_function' is denied for user 'test'")
3238+
"Execute access to function 'forbidden_function' is denied for user 'no_grants'")
32393239
require.Equal(t, terr.ExtraInfo.Errno, int64(0))
32403240
require.Equal(t, terr.ExtraInfo.Errcode, int64(42))
32413241
require.Equal(t,
32423242
terr.ExtraInfo.Fields,
32433243
map[interface{}]interface{}{
32443244
"object_type": "function",
32453245
"object_name": "forbidden_function",
3246-
"access_type": "Create",
3246+
"access_type": "Execute",
32473247
})
32483248
require.Nilf(t, terr.ExtraInfo.Prev, "stack contains exactly one error")
32493249
}

0 commit comments

Comments
 (0)