1
1
package crud
2
2
3
3
import (
4
+ "reflect"
4
5
"strings"
5
6
6
7
"github.com/vmihailenco/msgpack/v5"
@@ -21,6 +22,15 @@ type Error struct {
21
22
Stack string
22
23
// Str is the text of reason with error class.
23
24
Str string
25
+ // OperationData is the object/tuple with which an error occurred.
26
+ OperationData interface {}
27
+ // operationDataType contains the type of OperationData.
28
+ operationDataType reflect.Type
29
+ }
30
+
31
+ // newError creates an Error object with a custom operation data type to decoding.
32
+ func newError (operationDataType reflect.Type ) * Error {
33
+ return & Error {operationDataType : operationDataType }
24
34
}
25
35
26
36
// DecodeMsgpack provides custom msgpack decoder.
@@ -59,6 +69,20 @@ func (e *Error) DecodeMsgpack(d *msgpack.Decoder) error {
59
69
if e .Str , err = d .DecodeString (); err != nil {
60
70
return err
61
71
}
72
+ case "operation_data" :
73
+ if e .operationDataType != nil {
74
+ tuple := reflect .New (e .operationDataType )
75
+ if err = d .DecodeValue (tuple ); err != nil {
76
+ return err
77
+ }
78
+ e .OperationData = tuple .Elem ().Interface ()
79
+ } else {
80
+ var decoded interface {}
81
+ if err = d .Decode (& decoded ); err != nil {
82
+ return err
83
+ }
84
+ e .OperationData = decoded
85
+ }
62
86
default :
63
87
if err := d .Skip (); err != nil {
64
88
return err
@@ -77,6 +101,15 @@ func (e Error) Error() string {
77
101
// ErrorMany describes CRUD error object for `_many` methods.
78
102
type ErrorMany struct {
79
103
Errors []Error
104
+ // operationDataType contains the type of OperationData for each Error.
105
+ operationDataType reflect.Type
106
+ // isDecoded indicates whether the object has been decoded.
107
+ isDecoded bool
108
+ }
109
+
110
+ // newErrorMany creates an ErrorMany object with a custom operation data type to decoding.
111
+ func newErrorMany (operationDataType reflect.Type ) * ErrorMany {
112
+ return & ErrorMany {operationDataType : operationDataType }
80
113
}
81
114
82
115
// DecodeMsgpack provides custom msgpack decoder.
@@ -88,16 +121,15 @@ func (e *ErrorMany) DecodeMsgpack(d *msgpack.Decoder) error {
88
121
89
122
var errs []Error
90
123
for i := 0 ; i < l ; i ++ {
91
- var crudErr * Error = nil
124
+ crudErr := newError ( e . operationDataType )
92
125
if err := d .Decode (& crudErr ); err != nil {
93
126
return err
94
- } else if crudErr != nil {
95
- errs = append (errs , * crudErr )
96
127
}
128
+ errs = append (errs , * crudErr )
97
129
}
98
130
99
131
if len (errs ) > 0 {
100
- * e = ErrorMany { Errors : errs }
132
+ e . Errors = errs
101
133
}
102
134
103
135
return nil
0 commit comments