argonaut icon indicating copy to clipboard operation
argonaut copied to clipboard

Beginner question: error messages on decoding

Open leus opened this issue 5 years ago • 3 comments

I've been programming with Scala for a few months now, but I still don't understand how (and if) can I get a better error messages from Argonaut. Currently, I have a codec and I use it as this:

val json: String = ...
val parsed = Parse.decodeEither[MyClass](json)
val checks = if (parsed.isRight) parsed.right.get
else sys.error("Unable to parse MyClass json: " + parsed.left)

However, I never get any useful error messages when I use this. All I get is java.lang.RuntimeException: Unable to parse MyClass json: LeftProjection(Left(String: CursorHistory(List()))).

What is the proper way to handle decoding errors?

leus avatar Nov 15 '18 13:11 leus

Is this thing on?

leus avatar Dec 04 '18 18:12 leus

Yes, but it's not really the forum for questions like this. It would be easier to diagnose if we had some example JSON that you were experiencing an error with.

seanparsons avatar Dec 04 '18 18:12 seanparsons

I haven't been able to trace any better way of getting errors :/ There are multiple facets in this question, but I think it's good and I think it should be solved - it should probably be divided into multiple issues:

Typed error model Currently we only get strings out with the error, this is fine and dandy as long as the only thing one want to do is print it. However, this heavily impedes pattern matching of all kinds, it inhibits the collection of multiple error messages etc. (e.g, in { foo": 42, "bar": true there are two errors and not only one (missing " and unexpected ending of json).

An error model could be something along the lines of:

trait ArgonautError {
    val description: String
}

trait ParsingError extends ArgonautError {
    val lineNumber: Int
    val firstCharacterPosition: Int
}

final case class UexpectedEnd(val description: String, val lineNumber: Int, val firstCharacterPosition: Int) extends ParsingError

This talk covers a lot of the considerations and problems, and it might be very useful (it's been useful to me at least): https://www.youtube.com/watch?v=P8nGAo3Jp-Q

Parsing errors One level of error is in the parsing from text to Json. What additional and useful information can we pass along here? As hinted in the suggestion of a strongly typed model, I think line-number and the first character position of the place that cause the error is one option. Could also be the count of characters, so dealing with things like character sets, new line formats etc. is up to the user of argonauts. Perhaps other info would be good?

Decoding errors Decoding issues presents sort of another level of errors, since this is more about the issues arising when going from Json to some type T. But basically same question as before, what could be relevant here?

rohdef avatar Jun 15 '19 12:06 rohdef