Add better error messages to std/jsonutils (#20629)

* Add better error messages

* Add fmt on tuple msg
This commit is contained in:
Jake Leahy 2022-10-25 03:57:48 +11:00 • committed by GitHub
commit eed1b6df20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,7 +16,7 @@ runnableExamples:
assert 0.0.toJson.kind == JFloat assert 0.0.toJson.kind == JFloat
assert Inf.toJson.kind == JString assert Inf.toJson.kind == JString
import json, strutils, tables, sets, strtabs, options import json, strutils, tables, sets, strtabs, options, strformat
#[ #[
Future directions: Future directions:
@ -168,7 +168,7 @@ template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
if discKeys.len == 0 or hasField(oldObj, key): if discKeys.len == 0 or hasField(oldObj, key):
val = accessField(oldObj, key) val = accessField(oldObj, key)
else: else:
checkJson false, $($T, key, json) checkJson false, "key '$1' for $2 not in $3" % [key, $T, json.pretty()]
else: else:
if json.hasKey key: if json.hasKey key:
numMatched.inc numMatched.inc
@ -187,7 +187,7 @@ template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
else: else:
json.len == num and num == numMatched json.len == num and num == numMatched
checkJson ok, $(json.len, num, numMatched, $T, json) checkJson ok, "There were $1 keys (expecting $2) for $3 with $4" % [$json.len, $num, $T, json.pretty()]
proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions())
@ -227,7 +227,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
case b.kind case b.kind
of JInt: a = T(b.getBiggestInt()) of JInt: a = T(b.getBiggestInt())
of JString: a = parseEnum[T](b.getStr()) of JString: a = parseEnum[T](b.getStr())
else: checkJson false, $($T, " ", b) else: checkJson false, fmt"Expecting int/string for {$T} got {b.pretty()}"
elif T is uint|uint64: a = T(to(b, uint64)) elif T is uint|uint64: a = T(to(b, uint64))
elif T is Ordinal: a = cast[T](to(b, int)) elif T is Ordinal: a = cast[T](to(b, int))
elif T is pointer: a = cast[pointer](to(b, int)) elif T is pointer: a = cast[pointer](to(b, int))
@ -242,7 +242,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
case b.kind case b.kind
of JNull: a = nil of JNull: a = nil
of JString: a = b.str of JString: a = b.str
else: checkJson false, $($T, " ", b) else: checkJson false, fmt"Expecting null/string for {$T} got {b.pretty()}"
elif T is JsonNode: a = b elif T is JsonNode: a = b
elif T is ref | ptr: elif T is ref | ptr:
if b.kind == JNull: a = nil if b.kind == JNull: a = nil
@ -250,7 +250,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
a = T() a = T()
fromJson(a[], b, opt) fromJson(a[], b, opt)
elif T is array: elif T is array:
checkJson a.len == b.len, $(a.len, b.len, $T) checkJson a.len == b.len, fmt"Json array size doesn't match for {$T}"
var i = 0 var i = 0
for ai in mitems(a): for ai in mitems(a):
fromJson(ai, b[i], opt) fromJson(ai, b[i], opt)
@ -296,7 +296,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
for val in fields(a): for val in fields(a):
tupleSize.inc tupleSize.inc
checkJson b.len == tupleSize, $(b.len, tupleSize, $T, b) # could customize checkJson b.len == tupleSize, fmt"Json doesn't match expected length of {tupleSize}, got {b.pretty()}"
var i = 0 var i = 0
for val in fields(a): for val in fields(a):
fromJson(val, b[i], opt) fromJson(val, b[i], opt)