|
1 | 1 | package tarantool
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | +) |
| 6 | + |
3 | 7 | const error_extId = 3
|
4 | 8 |
|
5 | 9 | // BoxError is a type representing Tarantool `box.error` object: a single
|
@@ -27,7 +31,7 @@ func (err BoxError) Depth() (depth int) {
|
27 | 31 | return depth
|
28 | 32 | }
|
29 | 33 |
|
30 |
| -func decodeBoxErrorStack(d *decoder) (*BoxError, error) { |
| 34 | +func decodeBoxError(d *decoder) (*BoxError, error) { |
31 | 35 | var l, larr, l1, l2 int
|
32 | 36 | var errorStack []BoxError
|
33 | 37 | var err error
|
@@ -124,99 +128,99 @@ func decodeBoxErrorStack(d *decoder) (*BoxError, error) {
|
124 | 128 | return nil, nil
|
125 | 129 | }
|
126 | 130 |
|
127 |
| -// func encodeBoxError(enc *encoder, err BoxError) (*BoxError, error) { |
128 |
| -// var l, larr, l1, l2 int |
129 |
| -// var errorStack []BoxError |
130 |
| -// var err error |
131 |
| -// var mapk, mapv interface{} |
132 |
| - |
133 |
| -// if l, err = d.DecodeMapLen(); err != nil { |
134 |
| -// return nil, err |
135 |
| -// } |
136 |
| - |
137 |
| -// for ; l > 0; l-- { |
138 |
| -// var cd int |
139 |
| -// if cd, err = d.DecodeInt(); err != nil { |
140 |
| -// return nil, err |
141 |
| -// } |
142 |
| -// switch cd { |
143 |
| -// case KeyErrorStack: |
144 |
| -// if larr, err = d.DecodeArrayLen(); err != nil { |
145 |
| -// return nil, err |
146 |
| -// } |
147 |
| - |
148 |
| -// errorStack = make([]BoxError, larr) |
149 |
| - |
150 |
| -// for i := 0; i < larr; i++ { |
151 |
| -// if l1, err = d.DecodeMapLen(); err != nil { |
152 |
| -// return nil, err |
153 |
| -// } |
154 |
| - |
155 |
| -// for ; l1 > 0; l1-- { |
156 |
| -// var cd1 int |
157 |
| -// if cd1, err = d.DecodeInt(); err != nil { |
158 |
| -// return nil, err |
159 |
| -// } |
160 |
| -// switch cd1 { |
161 |
| -// case KeyErrorType: |
162 |
| -// if errorStack[i].Type, err = d.DecodeString(); err != nil { |
163 |
| -// return nil, err |
164 |
| -// } |
165 |
| -// case KeyErrorFile: |
166 |
| -// if errorStack[i].File, err = d.DecodeString(); err != nil { |
167 |
| -// return nil, err |
168 |
| -// } |
169 |
| -// case KeyErrorLine: |
170 |
| -// if errorStack[i].Line, err = d.DecodeInt64(); err != nil { |
171 |
| -// return nil, err |
172 |
| -// } |
173 |
| -// case KeyErrorMessage: |
174 |
| -// if errorStack[i].Message, err = d.DecodeString(); err != nil { |
175 |
| -// return nil, err |
176 |
| -// } |
177 |
| -// case KeyErrorErrno: |
178 |
| -// if errorStack[i].Errno, err = d.DecodeInt64(); err != nil { |
179 |
| -// return nil, err |
180 |
| -// } |
181 |
| -// case KeyErrorErrcode: |
182 |
| -// if errorStack[i].Errcode, err = d.DecodeInt64(); err != nil { |
183 |
| -// return nil, err |
184 |
| -// } |
185 |
| -// case KeyErrorFields: |
186 |
| -// errorStack[i].Fields = make(map[interface{}]interface{}) |
187 |
| -// if l2, err = d.DecodeMapLen(); err != nil { |
188 |
| -// return nil, err |
189 |
| -// } |
190 |
| -// for ; l2 > 0; l2-- { |
191 |
| -// if mapk, err = d.DecodeInterface(); err != nil { |
192 |
| -// return nil, err |
193 |
| -// } |
194 |
| -// if mapv, err = d.DecodeInterface(); err != nil { |
195 |
| -// return nil, err |
196 |
| -// } |
197 |
| -// errorStack[i].Fields[mapk] = mapv |
198 |
| -// } |
199 |
| -// default: |
200 |
| -// if err = d.Skip(); err != nil { |
201 |
| -// return nil, err |
202 |
| -// } |
203 |
| -// } |
204 |
| -// } |
205 |
| - |
206 |
| -// if i > 0 { |
207 |
| -// errorStack[i-1].Prev = &errorStack[i] |
208 |
| -// } |
209 |
| -// } |
210 |
| -// default: |
211 |
| -// if err = d.Skip(); err != nil { |
212 |
| -// return nil, err |
213 |
| -// } |
214 |
| -// } |
215 |
| -// } |
216 |
| - |
217 |
| -// if len(errorStack) > 0 { |
218 |
| -// return &errorStack[0], nil |
219 |
| -// } |
220 |
| - |
221 |
| -// return nil, nil |
222 |
| -// } |
| 131 | +func msgpackDecodeBoxError(d *decoder, v reflect.Value) (err error) { |
| 132 | + boxError, err := decodeBoxError(d) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + v.Set(reflect.ValueOf(boxError)) |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func encodeBoxError(enc *encoder, boxError BoxError) (err error) { |
| 142 | + if err = enc.EncodeArrayLen(1); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + if err = encodeUint(enc, KeyErrorStack); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + var stackDepth = boxError.Depth() |
| 150 | + if err = enc.EncodeArrayLen(stackDepth); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + for ; stackDepth > 0; stackDepth-- { |
| 155 | + if err = encodeUint(enc, KeyErrorType); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + if err = enc.EncodeString(boxError.Type); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + if err = encodeUint(enc, KeyErrorFile); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + if err = enc.EncodeString(boxError.File); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + if err = encodeUint(enc, KeyErrorLine); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + if err = enc.EncodeInt64(boxError.Line); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + if err = encodeUint(enc, KeyErrorFile); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + if err = enc.EncodeString(boxError.File); err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + if err = encodeUint(enc, KeyErrorMessage); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + if err = enc.EncodeString(boxError.Message); err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + if err = encodeUint(enc, KeyErrorErrno); err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + if err = enc.EncodeInt64(boxError.Errno); err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + if err = encodeUint(enc, KeyErrorErrcode); err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + if err = enc.EncodeInt64(boxError.Errcode); err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + |
| 204 | + if len(boxError.Fields) > 0 { |
| 205 | + if err = encodeUint(enc, KeyErrorFields); err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + for k, v := range boxError.Fields { |
| 210 | + if err = enc.Encode(k); err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + if err = enc.Encode(v); err != nil { |
| 214 | + return err |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return nil |
| 221 | +} |
| 222 | + |
| 223 | +func msgpackEncodeBoxError(e *encoder, v reflect.Value) (err error) { |
| 224 | + val := v.Interface().(BoxError) |
| 225 | + return encodeBoxError(e, val) |
| 226 | +} |
0 commit comments