Skip to content

Conversation

Lysxia
Copy link
Collaborator

@Lysxia Lysxia commented May 20, 2016

The first commit is not a direct fix, but gets rid of some redundancy in error messages in nested records.

In the definition of (.:) (as well as its siblings (.:?), etc.) the location information is provided twice.

    data X = {a :: (Int,Int,Int)}
    data Y = {x :: X}
    instance FromJSON X where
        parseJSON (Object obj) = X <$> obj .: "a"
    instance FromJSON Y where
        parseJSON (Object obj) = Y <$> obj .: "x"

This results in this kind of error when using decodeEither:

    > eitherDecode "{\"x\":{\"a\": [1,2,true]}}" :: Either String Y
    Left "Error in $.x.a[2]: failed to parse field x: failed to parse field a: expected Int, encountered Boolean"

The location is given in two different forms; the second one is extremely verbose and also incomplete, missing the [2].

This commit simply removes the use of modifyFailure in the combinators, so that only the compact path description is left.

Left "Error in $.x.a[2]: expected Int, encountered Boolean"

One thing that might be problematic with this solution is that the function parse :: (a -> Parser b) -> a -> Result b no longer has any location information by default since it discards the JSONPath parameter. OTOH some users might prefer to insert modifyFailure themselves to customize their error messages, which previously prevented the use of (.:).

@Lysxia
Copy link
Collaborator Author

Lysxia commented May 20, 2016

Oh wait, the Generic is still wrong.

@Lysxia
Copy link
Collaborator Author

Lysxia commented May 20, 2016

This is why I should test before submitting the PR.

@Lysxia
Copy link
Collaborator Author

Lysxia commented May 20, 2016

Fixes #357

@bergmark bergmark mentioned this pull request Jun 2, 2016
18 tasks
@bergmark
Copy link
Collaborator

bergmark commented Jun 4, 2016

Thank you for this! Looks very nice.

@bergmark bergmark merged commit f2f73cc into haskell:master Jun 4, 2016
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

Successfully merging this pull request may close these issues.

2 participants