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

Use try/except to catch error #917

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

superstar54
Copy link
Member

@superstar54 superstar54 commented Nov 7, 2024

Fix #916

The error from 916 is because the setup process stopped because the code failed to inspect the qe env. In this PR, we handle this gracefully so that it returns False when failed to inspect qe env.

Copy link

codecov bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 7.69231% with 12 lines in your changes missing coverage. Please review.

Project coverage is 66.97%. Comparing base (2924582) to head (3b654d1).

Files with missing lines Patch % Lines
src/aiidalab_qe/setup/codes.py 7.69% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #917      +/-   ##
==========================================
- Coverage   67.06%   66.97%   -0.10%     
==========================================
  Files          51       51              
  Lines        4715     4723       +8     
==========================================
+ Hits         3162     3163       +1     
- Misses       1553     1560       +7     
Flag Coverage Δ
python-3.11 66.97% <7.69%> (-0.10%) ⬇️
python-3.9 66.99% <7.69%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@superstar54
Copy link
Member Author

Very funny thing: the pre-commit and ruff conflict with each other.

return env_exist and "qe" == info["name"]
except subprocess.CalledProcessError:
# Handle cases where the conda list command fails
return False
Copy link
Member

@unkcpz unkcpz Nov 8, 2024

Choose a reason for hiding this comment

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

This may not correct, if the function return from exception, the exception will not propagate up.
The exception was handled by where the function _install who is calling qe_installed().

The change here will shadow docker build to show the exception message when it is failed.

I agree here we need capture the except case, but instead of return, the except is better to propagate up to handle, since that means the subprocess call is problematic.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the comment! I didn't consider docker when modifying the code. Could we have a short zoom chat on this?

Copy link
Member

Choose a reason for hiding this comment

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

At 2 pm?

@superstar54
Copy link
Member Author

As suggested by @unkcpz , if the cmd to inspect the env fails, we should raise the error and stop the installation.

Comment on lines +55 to +58
env_exist = get_qe_env().exists()

if not env_exist:
return False
Copy link
Member Author

Choose a reason for hiding this comment

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

Because we check if the conda env exists here, I didn't add the code to run the code cmd using the subprocess.

@superstar54 superstar54 requested a review from unkcpz November 8, 2024 16:41
Comment on lines +82 to +86
return False # noqa: TRY300
except Exception as error:
raise RuntimeError(
"Failed to check if Quantum Espresso is installed."
) from error
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return False # noqa: TRY300
except Exception as error:
raise RuntimeError(
"Failed to check if Quantum Espresso is installed."
) from error
except Exception as exc:
raise RuntimeError(
"Failed to check if Quantum Espresso is installed."
) from exc
else:
return False # noqa: TRY300

Also for the catched exception, I think there are two types of exceptions we know which are SubprocessCallError and JsonParseError. You can catch those two and for uncatched exceptions, use raise to propagated it up.

Copy link
Member

Choose a reason for hiding this comment

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

For TRY300, you can move it to else block, https://docs.astral.sh/ruff/rules/try-consider-else/

@unkcpz
Copy link
Member

unkcpz commented Nov 11, 2024

Very funny thing: the pre-commit and ruff conflict with each other.

What you mean pre-commit and ruff are conflict? We use ruff in pre-commit as linter.

@superstar54
Copy link
Member Author

superstar54 commented Nov 11, 2024

I added code required by ruff, in this commit, but pre-commit deleted it later. Then ruff check failed.

@unkcpz
Copy link
Member

unkcpz commented Nov 11, 2024

I added code required by ruff, in this commit, but pre-commit deleted it later. Then ruff check failed.

Because the else was supposed to add after try..except. It is not correct to early return with an else inside the try block.

Comment on lines +82 to +86
return False # noqa: TRY300
except Exception as error:
raise RuntimeError(
"Failed to check if Quantum Espresso is installed."
) from error
Copy link
Member

Choose a reason for hiding this comment

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

For TRY300, you can move it to else block, https://docs.astral.sh/ruff/rules/try-consider-else/

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.

Failed to install QE when installing the app form the App Store.
2 participants