Skip to content

Commit

Permalink
rpc: signal JSON marshalling errors through exception
Browse files Browse the repository at this point in the history
See neo-project/neo-modules#696. Unmarshaller is left unchanged to be
compatible.
  • Loading branch information
roman-khimov committed May 4, 2022
1 parent 7f8b259 commit 930c439
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions pkg/rpc/response/result/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,26 @@ func (r *Invoke) Finalize() {
// MarshalJSON implements json.Marshaler.
func (r Invoke) MarshalJSON() ([]byte, error) {
defer r.Finalize()
var st json.RawMessage
arr := make([]json.RawMessage, len(r.Stack))
var (
st json.RawMessage
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
err error
)
var data []byte
if (r.Stack[i].Type() == stackitem.InteropT) && iterator.IsIterator(r.Stack[i]) {
iteratorValues, truncated := iterator.Values(r.Stack[i], r.maxIteratorResultItems)
value := make([]json.RawMessage, len(iteratorValues))
for j := range iteratorValues {
value[j], err = stackitem.ToJSONWithTypes(iteratorValues[j])
if err != nil {
st = []byte(fmt.Sprintf(`"error: %v"`, err))
break
r.FaultException += fmt.Sprintf("%sjson error: %v", faultSep, err)
break arrloop
}
}
data, err = json.Marshal(iteratorAux{
Expand All @@ -123,15 +128,14 @@ func (r Invoke) MarshalJSON() ([]byte, error) {
} else {
data, err = stackitem.ToJSONWithTypes(r.Stack[i])
if err != nil {
st = []byte(fmt.Sprintf(`"error: %v"`, err))
r.FaultException += fmt.Sprintf("%sjson error: %v", faultSep, err)
break
}
}
arr[i] = data
}

var err error
if st == nil {
if err == nil {
st, err = json.Marshal(arr)
if err != nil {
return nil, err
Expand Down

0 comments on commit 930c439

Please sign in to comment.