Skip to content

User's JSON parse error causes ugly Python exception #809#1468

Merged
simobasso merged 3 commits intocookiecutter:masterfrom
noone234:handle-json-exception
Apr 29, 2021
Merged

User's JSON parse error causes ugly Python exception #809#1468
simobasso merged 3 commits intocookiecutter:masterfrom
noone234:handle-json-exception

Conversation

@noone234
Copy link
Copy Markdown
Contributor

@noone234 noone234 commented Oct 23, 2020

Addresses issue #809. A malformed JSON file no longer raises an unhandled exception.

Steps to reproduce the issue:

  1. Create a subtly malformed .json file in a project directory
  2. Run cookiecutter on that project
  3. Observe your eyes glazing over as a huge python exception comes out of it.

Cause:

In generate.py's generate_context() function, for a JSON decoding error, it raised a friendlier exception. That is still daunting to new users.

Solution:

Logs an error message and exits.

Other things to note:

The message text has not changed.

@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit 61175d3 and detected 0 issues on this pull request.

View more on Code Climate.

@insspb insspb added the WIP Work In Progress label Oct 25, 2020
@insspb
Copy link
Copy Markdown
Member

insspb commented Oct 25, 2020

@noone234 thank you for contribution. At least now I understand what is #809 about.

You should not remove current Raise error, as cc can be used in internal scripts and this error can be handled in some kind.
So when you remove raise - you making breaking change.

Correct way to implement this:
Detect is cc run as console app or as library.
If console app - you current solution.

If as part of other product - only raise, as it was.

@noone234 do you interested to continue work on this?

@insspb insspb added breaking-change Marks an important and likely breaking change. Require update for major version discussion labels Oct 25, 2020
@noone234
Copy link
Copy Markdown
Contributor Author

@insspb Thank you for reviewing my PR.

Yes, I am interested in continuing the work. I will try to do as you recommended.

@noone234 noone234 force-pushed the handle-json-exception branch from 61175d3 to c3b54c4 Compare October 27, 2020 00:25
Steps to reproduce the issue:
  Refer to cookiecutter#809
  1. Create a subtly malformed .json file in a project directory
  2. Run cookiecutter on that project
  3. Observe your eyes glazing over as a huge python exception comes out
     of it.

Cause:
  In generate.py's generate_context() function, for a JSON decoding error,
  it raised a friendlier exception.
  That is still daunting to new users.

Solution:
  cli.py's main() function already catches several exceptions.
  I added ContextDecodingException to that list.

  That makes cookiecutter print the friendlier error message
  and exit with a non-zero status.
@noone234 noone234 force-pushed the handle-json-exception branch from c3b54c4 to ead70e5 Compare October 27, 2020 00:26
@noone234
Copy link
Copy Markdown
Contributor Author

noone234 commented Oct 27, 2020

After studying the source code further, I opted for a simpler solution.

cli.py's main() function already catches several exceptions. I added ContextDecodingException to that list.

That has the desired effect.

@insspb
Copy link
Copy Markdown
Member

insspb commented Oct 27, 2020

@noone234
Nice.
Tests?

For this purpose, I:
  * Created "tests/fake-repo-bad-json" directory.
    In that directory,
    * "cookiecutter.json" is a copy of
      "tests/test-generate-context/invalid-syntax.json".
    * The rest is a copy of "tests/fake-repo-pre".
      That sufficed for this test.
@noone234 noone234 force-pushed the handle-json-exception branch from f8cdfee to c670df3 Compare October 28, 2020 00:29
@noone234
Copy link
Copy Markdown
Contributor Author

Added a test in test_cli.py. It is adapted from tests_generate_context.py's test_generate_context_with_json_decoding_error().

I am not sure if this suffices.

@noone234
Copy link
Copy Markdown
Contributor Author

Hi @insspb, I added a test in test_cli.py. Do you need anything else from me?

@insspb
Copy link
Copy Markdown
Member

insspb commented Oct 30, 2020

Hi @noone234 I am currently busy on other project. Will review after weekend.

@noone234
Copy link
Copy Markdown
Contributor Author

@insspb bumping this PR. It has been a few weeks since last activity.

@noone234
Copy link
Copy Markdown
Contributor Author

@insspb Can you please remove the breaking-change label from this PR? because it is no longer a breaking change.

Do you think you will still have time to review this PR? If not, what are my next steps?

Thank you.

@noone234
Copy link
Copy Markdown
Contributor Author

@JonZeolla Sorry to interrupt. Can you please help me get this PR unstuck or direct me to someone who can? There has been no activity since October; 4 months.

@noone234
Copy link
Copy Markdown
Contributor Author

@simobasso Hi, I'm reaching out for assistance. Can you please help me get this PR unstuck or direct me to someone who can? There has been no activity since October; 5 months.

Per the December 28 comment, can the breaking-change and wip labels be removed from this PR?

@simobasso
Copy link
Copy Markdown
Member

Hey @noone234, 👋 thank you for reaching out!

Sadly I don't have any permission to help you 😞 as you can see on #1485 I'm in the same conditions.

One of the maintainers said #1485 (comment) that no one has time for this project now.

What can I do for you is to review your pr but I cannot merge it sorry.

@simobasso simobasso added bug This issue/PR relates to a bug. and removed WIP Work In Progress breaking-change Marks an important and likely breaking change. Require update for major version labels Apr 27, 2021
@simobasso simobasso merged commit 3a82dcd into cookiecutter:master Apr 29, 2021
@simobasso
Copy link
Copy Markdown
Member

Hey @noone234 👋, just merged!

Thanks for your job and sorry 😅 about the delay, we will do better next time!

@noone234
Copy link
Copy Markdown
Contributor Author

noone234 commented May 5, 2021

Thank you so much @simobasso!

I see that you are now a maintainer. Congratulations! 🎉

@noone234 noone234 deleted the handle-json-exception branch May 5, 2021 19:51
@simobasso simobasso mentioned this pull request May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue/PR relates to a bug. discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants