elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
173 stars 17 forks source link

Wrong error message for json parsing in elm.json #264

Closed danfishgold closed 5 years ago

danfishgold commented 6 years ago

Description

When there's a parsing issue somewhere in elm.json, I get this error message when I run elm make Main.elm:

-- BAD JSON ----------------------------------------------------------- elm.json

Something went wrong while parsing your code.

1| {
  ^
I do not have any suggestions though!

Can you get it down to a <http://sscce.org> and share it at
<https://github.com/elm/error-message-catalog/issues>? That way we can figure
out how to give better advice!

I figured out eventually that I had an extra comma in my direct dependency list.

SSCCE

  1. elm init
  2. Add a trailing comma somewhere in elm.json
  3. elm make Main.elm

Side Note

The prompt in the error message to add this issue here is 100% the reason I did it. It's wonderful!

evancz commented 5 years ago

Thank you for the report! I am finishing up a bunch of work on the parser, so it is much easier to give precise errors for JSON now. I made a tweak in based on your specific case though, so my development build is saying:

-- EXTRA COMMA -------------------------------------------------------- elm.json

I ran into a problem with your elm.json file. I was partway through parsing a
JSON object when I got stuck here:

20|         },
            ^
I saw a comma right before I got stuck here, so I was expecting to see a field
name like "type" or "dependencies" next.

This error is commonly caused by trailing commas in JSON objects. Those are
actually disallowed by <https://json.org> so check the previous line for a
trailing commas that may need to be deleted.

Note: Here is an example of a valid JSON object for reference:

    {
      "name": "Tom",
      "age": 42
    }

Notice that (1) the field names are in double quotes and (2) there is no
trailing comma after the last entry. Both are strict requirements in JSON!

I would really like if it included the line above as well though. Let me see if I can add that in as well.

evancz commented 5 years ago

Okay, I made another little tweak to make it produce this:

-- EXTRA COMMA -------------------------------------------------------- elm.json

I ran into a problem with your elm.json file. I was partway through parsing a
JSON object when I got stuck here:

18|             "elm-explorations/markdown": "1.0.0",
19|             "rtfeldman/elm-iso8601-date-strings": "1.1.0",
20|         },
            ^
I saw a comma right before I got stuck here, so I was expecting to see a field
name like "type" or "dependencies" next.

This error is commonly caused by trailing commas in JSON objects. Those are
actually disallowed by <https://json.org> so check the previous line for a
trailing commas that may need to be deleted.

Note: Here is an example of a valid JSON object for reference:

    {
      "name": "Tom",
      "age": 42
    }

Notice that (1) the field names are in double quotes and (2) there is no
trailing comma after the last entry. Both are strict requirements in JSON!

These fixes should be available once Elm 0.19.1 is released. I think it is kind of fun to have really excellent JSON errors (since most parsers are made for speed) so please let me know if you find any more weird ones once you can try out the new messages!

danfishgold commented 5 years ago

Thanks Evan, that's really nice 😄 There's just one tiny typo: "so check the previous line for a trailing commas"

evancz commented 5 years ago

Oops! Should be fixed by https://github.com/elm/compiler/commit/3698de0101fc6ab058eace2e91f28cf11e3f4967