Merge pull request #2461 from nspcc-dev/rpc-json-error-in-exception

rpc: signal JSON marshalling errors through exception
This commit is contained in:
Roman Khimov 2022-05-04 17:22:05 +03:00 committed by GitHub
commit 58bda8be78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -95,21 +95,26 @@ func (r *Invoke) Finalize() {
// MarshalJSON implements json.Marshaler. // MarshalJSON implements json.Marshaler.
func (r Invoke) MarshalJSON() ([]byte, error) { func (r Invoke) MarshalJSON() ([]byte, error) {
defer r.Finalize() defer r.Finalize()
var st json.RawMessage
arr := make([]json.RawMessage, len(r.Stack))
for i := range arr {
var ( var (
data []byte st json.RawMessage
err error err error
faultSep string
arr = make([]json.RawMessage, len(r.Stack))
) )
if len(r.FaultException) != 0 {
faultSep = " / "
}
arrloop:
for i := range arr {
var data []byte
if (r.Stack[i].Type() == stackitem.InteropT) && iterator.IsIterator(r.Stack[i]) { if (r.Stack[i].Type() == stackitem.InteropT) && iterator.IsIterator(r.Stack[i]) {
iteratorValues, truncated := iterator.Values(r.Stack[i], r.maxIteratorResultItems) iteratorValues, truncated := iterator.Values(r.Stack[i], r.maxIteratorResultItems)
value := make([]json.RawMessage, len(iteratorValues)) value := make([]json.RawMessage, len(iteratorValues))
for j := range iteratorValues { for j := range iteratorValues {
value[j], err = stackitem.ToJSONWithTypes(iteratorValues[j]) value[j], err = stackitem.ToJSONWithTypes(iteratorValues[j])
if err != nil { if err != nil {
st = []byte(fmt.Sprintf(`"error: %v"`, err)) r.FaultException += fmt.Sprintf("%sjson error: %v", faultSep, err)
break break arrloop
} }
} }
data, err = json.Marshal(iteratorAux{ data, err = json.Marshal(iteratorAux{
@ -123,15 +128,14 @@ func (r Invoke) MarshalJSON() ([]byte, error) {
} else { } else {
data, err = stackitem.ToJSONWithTypes(r.Stack[i]) data, err = stackitem.ToJSONWithTypes(r.Stack[i])
if err != nil { if err != nil {
st = []byte(fmt.Sprintf(`"error: %v"`, err)) r.FaultException += fmt.Sprintf("%sjson error: %v", faultSep, err)
break break
} }
} }
arr[i] = data arr[i] = data
} }
var err error if err == nil {
if st == nil {
st, err = json.Marshal(arr) st, err = json.Marshal(arr)
if err != nil { if err != nil {
return nil, err return nil, err