question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pretty-printing of decoding failure history

See original GitHub issue

From gitter: https://gitter.im/travisbrown/circe?at=56b1d0786b6468374a0b2b7c

Consider the following example:

scala> val json = parse("""[1, 2, "hello", 4, "world", 6]""").getOrElse(Json.empty)
scala> Decoder[List[Int]].apply(json.hcursor)
res5: io.circe.Decoder.Result[List[Int]] = Left(io.circe.DecodingFailure: Int: El(MoveRight,true),El(MoveRight,true),El(DownArray,true))

This part might be hard to parse for a human eye:

io.circe.DecodingFailure: Int: El(MoveRight,true),El(MoveRight,true),El(DownArray,true)

I guess we could improve the error message by reversing a history, making it a bit more visual, and maybe adding the element we failed to parse. Something along these lines:

[↴ array]
  →|→ × Failed to decode hello as Int

I’m not sure about arrow symbols though (on github they are very tiny), so maybe just printing

[down array]
  [right], [right] × Failed to decode hello as Int

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:8
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
artempyanykhcommented, Mar 9, 2016

I see that CursorOp already has Show instance defined, and string representations of operators are already pretty visual there.

@tobias-johansson in his PR:

  1. collapsed array traversals which is very reasonable since you hardly ever want to see 27 arrows on the screen -> instead of [27], and
  2. changed the repr. of downField from --\\(field)" to .field.

These 2 things probably covers 80% of use-cases. For other Ops it just fallbacks to the already defined Show. Which gives us pretty much what I wanted to do minus a couple extra line-breaks 😃

@travisbrown do you think that going for the original output format is still relevant (comparing them side by side)?

[↴ array]
  →|→|→|→|→|→|→|→|→|→|→|→ × Int
// and
Decoding Failure at [12]: Int

From https://github.com/travisbrown/circe/pull/222#issuecomment-194017772,

DecodingFailure at .bar.baz.qux.a: String

looks really nice! What might be cool though is adding a current focus to the error message so it gives a bit more context

DecodingFailure at .bar.baz.qux.a: decoding 10 as String
0reactions
travisbrowncommented, Mar 8, 2016

Quoting myself from Gitter:

@tobias-johansson That sounds like reasonable output for Show[DecodingFailure]. I think it’d make sense to have the visualization that @ArtemPyanykh describes in #188 be available as something like a draw method on DecodingFailure, while the Show instance gives the more compact representation you describe.

So while #222 is merged, I think we should leave this issue open for the original more visual output proposed by @ArtemPyanykh.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve recursive decoding in Circe when parsing Json?
You can print the history like this (or just inspect it in the string ... DecodingFailure scala> io.circe.jawn.decode[Tasks](doc) match ...
Read more >
json — JSON encoder and decoder — Python 3.11.1 ...
A malicious JSON string may cause the decoder to consume considerable CPU and memory ... Using json.tool from the shell to validate and...
Read more >
circe/circe - Gitter
I'm trying to write a custom Decoder. Due to some unique 3rd party created constraints in the structure of the Json I'm getting,...
Read more >
Pretty Printing JSON Web Tokens (JWTs) on the Command ...
How to easily introspect and pretty print a signed JWT (JWS) or an encrypted JWT (JWE) on the ... Printf("Error decoding string: %s...
Read more >
JSON Decode Online - Code Beautify
What can you do with json_decode? · How to Create JSON File? · JSON Full Form · What is JSON? · JSON Example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found