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

Fix exception handling in commands calling list_repository_contents #3968

Merged
merged 1 commit into from
Apr 27, 2020

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Apr 25, 2020

Fixes #3967

The commands verdi node repo ls and verdi calcjob inputls/outputls
were passing the caught exception object straight into the echo call
without casting it to a string first, which would incur another
exception by itself. On top of that, they were catching ValueError
exceptions, which, as far as I can tell, would never be thrown. Instead
if the path does not exist a FileNotFoundError will be thrown. The
commands now catch this instead and print a useful message. This failure
path of the commands was not tested, so the bug went unnoticed.

The problem of raw exception objects being passed to echo_critical
were present in some other commands as well, which have now been fixed.

The commands `verdi node repo ls` and `verdi calcjob inputls/outputls`
were passing the caught exception object straight into the echo call
without casting it to a string first, which would incur another
exception by itself. On top of that, they were catching `ValueError`
exceptions, which, as far as I can tell, would never be thrown. Instead
if the path does not exist a `FileNotFoundError` will be thrown. The
commands now catch this instead and print a useful message. This failure
path of the commands was not tested, so the bug went unnoticed.

The problem of raw exception objects being passed to `echo_critical`
were present in some other commands as well, which have now been fixed.
@codecov
Copy link

codecov bot commented Apr 25, 2020

Codecov Report

Merging #3968 into develop will increase coverage by 0.00%.
The diff coverage is 75.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #3968   +/-   ##
========================================
  Coverage    78.33%   78.33%           
========================================
  Files          461      461           
  Lines        34076    34076           
========================================
+ Hits         26692    26694    +2     
+ Misses        7384     7382    -2     
Flag Coverage Δ
#django 70.38% <75.00%> (+0.02%) ⬆️
#sqlalchemy 71.23% <75.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
aiida/cmdline/utils/repository.py 88.88% <ø> (ø)
aiida/orm/nodes/node.py 91.21% <ø> (ø)
aiida/cmdline/commands/cmd_calcjob.py 68.42% <50.00%> (+1.50%) ⬆️
aiida/cmdline/commands/cmd_daemon.py 56.14% <100.00%> (ø)
aiida/cmdline/commands/cmd_export.py 92.50% <100.00%> (ø)
aiida/cmdline/commands/cmd_node.py 81.66% <100.00%> (+1.66%) ⬆️
aiida/orm/utils/log.py 76.66% <0.00%> (-16.67%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 62e2972...a967ebd. Read the comment docs.

Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

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

thanks for the fixes and for adding the tests - some comments/questions

@@ -176,8 +176,8 @@ def calcjob_inputls(calcjob, path, color):

try:
list_repository_contents(calcjob, path, color)
except ValueError as exception:
echo.echo_critical(exception)
except FileNotFoundError:
Copy link
Member

Choose a reason for hiding this comment

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

the only comment I have is that this might become a different exception when we move to the new repository implementation.
is there perhaps a higher-level exception that we could catch here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests I have added will then exactly catch that change and we can adapt it. Don't see the point in trying to anticipate the type of exception that this might throw in the future. Or did you mean to catch a more generic exception like OSError which is the base class of FileNotFoundError?

Copy link
Member

@ltalirz ltalirz Apr 27, 2020

Choose a reason for hiding this comment

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

right, I meant more generic.
Anyhow, if you think we should just use the one we have currently it's also ok. the tests will fail if this changes, so it will be fixed when it changes.

@@ -71,7 +71,7 @@ def start(foreground, number):
subprocess.check_output(command, env=currenv, stderr=subprocess.STDOUT) # pylint: disable=unexpected-keyword-arg
except subprocess.CalledProcessError as exception:
click.secho('FAILED', fg='red', bold=True)
echo.echo_critical(exception.output)
echo.echo_critical(str(exception))
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used str since that is what is intended for normal users, whereas repr is for debugging purposes. I think in this case, showing the exception type for the user is not necessary. But if you think this would actually be better, happy to change it

Copy link
Member

Choose a reason for hiding this comment

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

I see... up to you.

Copy link
Member

@ramirezfranciscof ramirezfranciscof left a comment

Choose a reason for hiding this comment

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

Looks good to me. Nice catch!

@ltalirz
Copy link
Member

ltalirz commented Apr 27, 2020

good to go from me as well

@sphuber sphuber merged commit 98e21da into aiidateam:develop Apr 27, 2020
@sphuber sphuber deleted the fix/3967/verdi-repo-ls branch April 28, 2020 12:38
@sphuber sphuber modified the milestone: v1.2.2 May 20, 2020
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.

Commands verdi node repo ls and verdi calcjob inputls/outputls will not catch correct exception
3 participants