state: move nil check down to stackitem JSON processing

We want to return real errors, not some generic thing for any kind of thing
happening.
This commit is contained in:
Roman Khimov 2021-07-07 00:38:19 +03:00
parent 5a9efcc654
commit e62a766058
3 changed files with 12 additions and 9 deletions

View file

@ -232,10 +232,6 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) {
if len(seen) > MaxJSONDepth {
return "", ErrTooDeep
}
typ := item.Type()
result := map[string]interface{}{
"type": typ.String(),
}
var value interface{}
switch it := item.(type) {
case *Array, *Struct:
@ -281,6 +277,11 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) {
delete(seen, item)
case *Pointer:
value = it.pos
case nil:
return "", fmt.Errorf("%w: nil", ErrUnserializable)
}
result := map[string]interface{}{
"type": item.Type().String(),
}
if value != nil {
result["value"] = value