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

Be clearer when there is an error with the user supplied json file #78

Closed
patrickfuchs opened this issue Apr 24, 2021 · 2 comments · Fixed by #80
Closed

Be clearer when there is an error with the user supplied json file #78

patrickfuchs opened this issue Apr 24, 2021 · 2 comments · Fixed by #80

Comments

@patrickfuchs
Copy link
Owner

Making a try with a toy (user supplied) json file:

$ buildH -c 3Conly.pdb -d 3Conly.def -lt Berger_3Conly.json  -l PO3C
usage: buildH [-h] -c COORD [-t TRAJ] -l LIPID [-lt LIPID_TOPOLOGY [LIPID_TOPOLOGY ...]] -d DEFOP [-opx OPDBXTC] [-o OUT]
              [-b BEGIN] [-e END] [-pi PICKLE]
buildH: error: Berger_3Conly.json is in a bad format.

This error message is not very precise. The user can't find where the error comes from in the json file.

When doing this by hand:

>>> f = open("Berger_3Conly.json")
>>> json.load(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fuchs/software/miniconda3/envs/buildh/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/home/fuchs/software/miniconda3/envs/buildh/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/home/fuchs/software/miniconda3/envs/buildh/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/fuchs/software/miniconda3/envs/buildh/lib/python3.9/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1 (char 61)

Here we can see that it comes from line 4 column 1. Easier to debug.
In fact the error was coming from a comma at the end of the last line :

{
    "resname": ["PO3C"],
    "C26": ["CH2", "C25", "C27"],
}

It makes me think to warn the user about this: don't put a comma on the last line!

@patrickfuchs patrickfuchs changed the title Be clearer when there is an error with the used supplied json file Be clearer when there is an error with the user supplied json file Apr 24, 2021
@HubLot
Copy link
Collaborator

HubLot commented Apr 26, 2021

Yes, I hide the traceback of the exception thrown by the JSON parser. I thought it would make the output unreadable with uninformative lines. JSON format is pretty well-known so I thought it will be easy to spot a mistake in it.

We can keep the traceback if you think it's not enough.

@patrickfuchs
Copy link
Owner Author

I agree the Traceback is polluted by too much text. But on the other hand, knowing the problem comes from line x column y is very useful in a file with a few dozens of lines. If we could keep only the last line json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1 (char 61) which is informative, that would be ideal. Don't know if it's doable though.

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 a pull request may close this issue.

2 participants