summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJake Leahy <jake@leahy.dev>2022-10-25 03:57:48 +1100
committerGitHub <noreply@github.com>2022-10-24 12:57:48 -0400
commiteed1b6df20db6f73825eefaab6d7626acdf3a6af (patch)
tree918c3736d28ecf6d055d1d125d2d21119728438f /lib
parente2f412145f30e3d68eb58fd1456f48de2e0b6614 (diff)
downloadNim-eed1b6df20db6f73825eefaab6d7626acdf3a6af.tar.gz
Add better error messages to `std/jsonutils` (#20629)
* Add better error messages

* Add fmt on tuple msg
Diffstat (limited to 'lib')
-rw-r--r--lib/std/jsonutils.nim14
1 files changed, 7 insertions, 7 deletions
diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim
index 05f9f661c..49367dac3 100644
--- a/lib/std/jsonutils.nim
+++ b/lib/std/jsonutils.nim
@@ -16,7 +16,7 @@ runnableExamples:
   assert 0.0.toJson.kind == JFloat
   assert Inf.toJson.kind == JString
 
-import json, strutils, tables, sets, strtabs, options
+import json, strutils, tables, sets, strtabs, options, strformat
 
 #[
 Future directions:
@@ -168,7 +168,7 @@ template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
           if discKeys.len == 0 or hasField(oldObj, key):
             val = accessField(oldObj, key)
       else:
-        checkJson false, $($T, key, json)
+        checkJson false, "key '$1' for $2 not in $3" % [key, $T, json.pretty()]
     else:
       if json.hasKey key:
         numMatched.inc
@@ -187,7 +187,7 @@ template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
     else:
       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())
 
@@ -227,7 +227,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
     case b.kind
     of JInt: a = T(b.getBiggestInt())
     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 Ordinal: a = cast[T](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
     of JNull: a = nil
     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 ref | ptr:
     if b.kind == JNull: a = nil
@@ -250,7 +250,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
       a = T()
       fromJson(a[], b, opt)
   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
     for ai in mitems(a):
       fromJson(ai, b[i], opt)
@@ -296,7 +296,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
         for val in fields(a):
           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
       for val in fields(a):
         fromJson(val, b[i], opt)
'n239' href='#n239'>239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392