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

add error handling to env vars #43

Merged

Conversation

pmayd
Copy link
Collaborator

@pmayd pmayd commented Sep 7, 2020

I found it confusing that the env vars are not checked and lead to non obvious errors like failed data connection because of "NoneType" error. So I added a helper function to parse env vars and inform the user if the expected env var is missing or if it is found but empty. This improves error handling and helps the user understand where the env vars are needed and how to fix errors.

First small contribution.

@pmayd pmayd force-pushed the improve_readin_env_vars branch from 5504f75 to 44d9eca Compare September 7, 2020 22:10
@bbelderbos
Copy link
Collaborator

Nice. So do I merge this one first, then you merge forward pmayd:improve_joke_command so this change is not visible there anymore @pmayd ?

@pmayd
Copy link
Collaborator Author

pmayd commented Sep 8, 2020

I don't think so, PR are indepent of each other. So if you merge this one the second will no longer be mergeable without conflicts if the same file is targeted. Otherwise github should merge both without problems because when you merge the second PR, it will be compared with the current code base which then already contains the first PR.

However, as we have discussed I will add tests to the joke PR so I can also update from the dev branch of there are recent changes/updatrs

@pogross
Copy link
Member

pogross commented Sep 8, 2020

If #43 is good to go merge it and @pmayd can pull from upstream to include all the changes in #45

Should be easy without conflicts as settings.py is not involved in the #45 changes

@bbelderbos
Copy link
Collaborator

Yep sounds good, thanks

@bbelderbos bbelderbos merged commit e793c8b into PyBites-Open-Source:develop Sep 8, 2020
@pogross
Copy link
Member

pogross commented Sep 8, 2020

This broke the tests. Why did CI not catch this?

============================= test session starts ==============================
platform linux -- Python 3.7.9, pytest-5.4.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/runner/work/karmabot/karmabot
plugins: cov-2.10.0, mock-3.2.0
collected 0 items / 1 error

==================================== ERRORS ====================================
___________________ ERROR collecting tests/test_karmabot.py ____________________
tests/test_karmabot.py:10: in <module>
    import karmabot.commands.topchannels
src/karmabot/commands/topchannels.py:12: in <module>
    from karmabot.settings import SLACK_CLIENT
src/karmabot/settings.py:36: in <module>
    DATABASE_URL = _get_env_var("KARMABOT_DATABASE_URL")
src/karmabot/settings.py:16: in _get_env_var
    f"{env_var} was not found. Please check your .karmabot file as well as the README.md."
E   KeyError: 'KARMABOT_DATABASE_URL was not found. Please check your .karmabot file as well as the README.md.'
------------------------------- Captured stderr --------------------------------
09-08 20:03 root         INFO     Script started

@pmayd
Copy link
Collaborator Author

pmayd commented Sep 8, 2020

Hm really? Running pytest locally did not fail, so it's something with the github runners?

Of course the file karmabot is not available so how do you pass the envs to the tests on ci? But yes, ci should have failed

@pmayd
Copy link
Collaborator Author

pmayd commented Sep 8, 2020

Hm I see the envs are defined in the noxfile so why does the test say it is not available?

@pmayd
Copy link
Collaborator Author

pmayd commented Sep 8, 2020

OK because database is missing from the vars... Getting late here. So I assume before my pr the database env was empty but no test uses the dB connection so it was no deal but now it is looking for the env so I fix the nox config file with a dummy entry

@pogross
Copy link
Member

pogross commented Sep 8, 2020

pytest fails too, locally

❯ pytest
============================================================================================== test session starts ==============================================================================================
platform linux -- Python 3.7.9, pytest-5.4.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/patrick/workspace/karmabot
plugins: mock-3.2.0, cov-2.10.0
collected 0 items / 1 error

==================================================================================================== ERRORS =====================================================================================================
____________________________________________________________________________________ ERROR collecting tests/test_karmabot.py ____________________________________________________________________________________
tests/test_karmabot.py:10: in <module>
    import karmabot.commands.topchannels
src/karmabot/commands/topchannels.py:12: in <module>
    from karmabot.settings import SLACK_CLIENT
src/karmabot/settings.py:33: in <module>
    KARMABOT_ID = _get_env_var("KARMABOT_SLACK_USER")
src/karmabot/settings.py:16: in _get_env_var
    f"{env_var} was not found. Please check your .karmabot file as well as the README.md."
E   KeyError: 'KARMABOT_SLACK_USER was not found. Please check your .karmabot file as well as the README.md.'
------------------------------------------------------------------------------------------------ Captured stderr ------------------------------------------------------------------------------------------------
09-08 22:30 root         INFO     Script started
============================================================================================ short test summary info ============================================================================================
ERROR tests/test_karmabot.py - KeyError: 'KARMABOT_SLACK_USER was not found. Please check your .karmabot file as well as the README.md.'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================================== 1 error in 0.25s ================================================================================================

bbelderbos added a commit that referenced this pull request Sep 9, 2020
* bump version for pypi package generation to match git tag

* Fix version (#40)

* add runtime file for heroku

* allow any 3.7 sub version

* breaks heroku deploy, poetry build pack makes this file

* dummy commit

* version bump

* Update README (#42)

* Add some Slack and Docker detail to the README

Provide some more guidance around finding Slack IDs and tokens, and
running the included docker-based postgres server.

* Remove readme detail

* Allow `topchannels` command in private messages (#41)

Let people review the top channel list in the peace and comfort of their
own DMs :)

* add error handling for env vars; run pre-commit (#43)

* fix python version and enable test for PRs

* add a fake KARMABOT_DATABASE_URL to the nox env (#48)

* Improve joke command (#45)

* add error handling for env vars; run pre-commit

* improve joke command (issue #37): get list of categories from pyjokes module and find closest match with difflib

* refactor logic of category match into helper function with own tests

* version bump

* Update versions of python, nox, poetry

Co-authored-by: AJ Kerrigan <[email protected]>
Co-authored-by: Michael Aydinbas <[email protected]>
Co-authored-by: Patrick-Oliver Groß <[email protected]>
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.

3 participants