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

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

Closed
danfishgold opened this issue Aug 25, 2018 · 4 comments
Closed

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

danfishgold opened this issue Aug 25, 2018 · 4 comments
Labels

Comments

@danfishgold
Copy link

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

evancz commented Jul 25, 2019

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 added a commit to elm/compiler that referenced this issue Jul 25, 2019
Some specific errors benefit from having the line above, like in
elm/error-message-catalog#264

Since the JSON syntax errors are always for a single position (no
context stack!) I figured I did a fix that just includes two extra
lines for all JSON syntax errors. This should not be too disruptive and
should give more of a feeling of place within the JSON file, especially
when it's a single line with something weird on it!
@evancz
Copy link
Member

evancz commented Jul 25, 2019

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

danfishgold commented Jul 26, 2019

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

@evancz
Copy link
Member

evancz commented Jul 26, 2019

Oops! Should be fixed by elm/compiler@3698de0

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

No branches or pull requests

2 participants