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

Do not ignore exception while reading configuration file #260

Merged
merged 7 commits into from
Mar 29, 2016

Conversation

ael-code
Copy link
Member

@ael-code ael-code commented Mar 7, 2016

Except for some cleanup and refactoring the main change is bring by the second commit, and it should fix #255.

It does not matter how the configuration file path is given (env, cli option, etc...), if there is an error while opening that file, an exception is raised.
The cli will print the stack trace if in debug mode otherwise it will print a colored one-line message.

There are no test on cli side, so please make some manual test.

@ael-code ael-code changed the title Conf file failures Do not ignore exception while reading configuration file Mar 7, 2016
@boyska
Copy link
Member

boyska commented Mar 7, 2016

I guess that @leophys won the right to test this pull request raising #255 :P

@boyska
Copy link
Member

boyska commented Mar 15, 2016

Please note: I have only read the code, but not tested it

Could you create a unit test in which the configuration file exists but is not valid json?

Also, I don't really like how we are using exit() in cli/; we'd better raise ClickException instead: those are testable, exits are not. Therefore, would you change die with raise ?

@leophys
Copy link
Member

leophys commented Mar 16, 2016

I tested this pull req with an empty json configuration file an a malformed one (i.e. a file
containing only {,}). In the first case libreant properly fails, warning about the impossibility to read a proper json file. In the second it fails signaling where the file is not properly formatted but such error does not clearly indicate the path of the file that causes the failure. I suppose this is not a problem, as far as this is the only json involved in the startup process of libreant.

I am going to merge this pull request, as far as there are no objections, and would open another issue to signal the need for a proper test unit, as signaled by @boyska

@ael-code
Copy link
Member Author

On 03/16/2016 03:28 PM, Blallo wrote:

I tested this pull req with an empty json configuration file an a
malformed one (i.e. a file
containing only |{,}|). In the first case libreant properly fails,
warning about the impossibility to read a proper json file. In the
second it fails signaling where the file is not properly formatted but
such error does not clearly indicate the path of the file that causes
the failure. I suppose this is not a problem, as far as this is the only
json involved in the startup process of libreant.

I am going to merge this pull request, as far as there are no
objections, and would open another issue to signal the need for a proper
test unit, as signaled by @boyska https://github.com/boyska

please give me some more time to analyze it.

@leophys
Copy link
Member

leophys commented Mar 16, 2016

Sorry @ael-code, I don't get it. Do you think there is something wrong in your pull request? =)

try:
with open(fname) as buf:
conf = json.load(buf)
return conf
except Exception:
log.warning('Error loading config file', exc_info=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding fname to this warning would probably fix the problem raised by @leophys

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said a stupid thing. You removed that exception handling. Instead, you only catch EnvironmentError, which is what happens when, for example, you don't have permissions.

You should add an except ValueError clause

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 03/16/2016 04:12 PM, BoySka wrote:

In conf/config_utils.py
#260 (comment):

 try:
     with open(fname) as buf:
         conf = json.load(buf)
         return conf
  • except Exception:
  •    log.warning('Error loading config file', exc_info=1)
    

I said a stupid thing. You /removed/ that exception handling. Instead,
you only catch |EnvironmentError|, which is what happens when, for
example, you don't have permissions.

Hahhah I've tried exactly the same thing when I've coded it. This is the
reason why the name it is not printed here.

You should add an |except ValueError| clause

using the wrapper we have cleaner code and
consitent behaviour among all commands
@ael-code
Copy link
Member Author

I tested this pull req with an empty json configuration file an a malformed one (i.e. a file
containing only {,}). In the first case libreant properly fails, warning about the impossibility to read a proper json file. In the second it fails signaling where the file is not properly formatted but such error does not clearly indicate the path of the file that causes the failure. I suppose this is not a problem, as far as this is the only json involved in the startup process of libreant.

Thanks for the very clear explanation. Unfortunately it could be the case that conf file isn't the only json involved. I've added a specific error log line in which the path of the file is printed along with the reason why it is malformed.
So in this case a log is emitted and then a ValueError is raised. The reason for the additional log message is because it is not so easy to modify exception on python 2

Also, I don't really like how we are using exit() in cli/; we'd better raise ClickException instead: those are testable, exits are not. Therefore, would you change die with raise ?

Done. We now have:

  • die(): raise click.ClickException with a custom exit_code and the message will be rendered in red
  • bye(): print colored message (you can choose the color) and exit with the custom exit_code. This is used for the cases like "no results were found"

I really don't like this division but exception handling of Click sucks. This is the best solution I've found.

Could you create a unit test in which the configuration file exists but is not valid json?

Done

@ael-code
Copy link
Member Author

@leophys any news on this?

@leophys leophys merged commit b09d69e into insomnia-lab:dev Mar 29, 2016
@ael-code ael-code deleted the conf-file-failures branch April 3, 2016 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Libreant starts also if it fails to read the conf file
3 participants