Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginner question: error messages on decoding #317

Open
leus opened this issue Nov 15, 2018 · 3 comments
Open

Beginner question: error messages on decoding #317

leus opened this issue Nov 15, 2018 · 3 comments

Comments

@leus
Copy link

leus commented Nov 15, 2018

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
Copy link
Author

leus commented Dec 4, 2018

Is this thing on?

@seanparsons
Copy link
Member

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.

@rohdef
Copy link

rohdef commented Jun 15, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@leus @seanparsons @rohdef and others