-
-
Notifications
You must be signed in to change notification settings - Fork 227
✨ Add TaskError exception for subprocess management #1991
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution, @tsvikas! 🙏
I have two minor suggestions (see inline comment).
IIUC, a failing task will raise the TaskError exception and the message will be shown, but the error from the task command will be hidden when running Copier via CLI, as it's not printed anymore but only accessible via the stdout/stderr attributes of the exception object. I think we should catch this exception in
Lines 66 to 80 in a731d2b
| def _handle_exceptions(method: Callable[[], None]) -> int: | |
| """Handle keyboard interruption while running a method.""" | |
| try: | |
| try: | |
| method() | |
| except KeyboardInterrupt: | |
| raise UserMessageError("Execution stopped by user") | |
| except UserMessageError as error: | |
| print(colors.red | "\n".join(error.args), file=sys.stderr) | |
| return 1 | |
| except UnsafeTemplateError as error: | |
| print(colors.red | "\n".join(error.args), file=sys.stderr) | |
| # DOCS https://github.com/copier-org/copier/issues/1328#issuecomment-1723214165 | |
| return 0b100 | |
| return 0 |
and print the message plus stdout/stderr.
Actually, the So we only need to output the message itself ( |
|
I did all of the requested changes, except inheriting from |
|
Done :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tsvikas I have a few minor remarks.
@copier-org/maintainers What is the correct use of UserMessageError? When I check its occurrences, it seems this error is directly raised when it originates from an improper use of Copier that can be fixed by the user. One counter-example I've seen is a missing --unsafe/--trust flag which raises UnsafeTemplateError (which isn't a subclass of UserMessageError), although this problem can be resolved by a Copier user by adding the flag. What about a task error? Is it an error caused mainly by a template author which can't be fixed by a user? Is subclassing UserMessageError appropriate in this case? WDYT?
personally i assumed that |
|
Hey @sisp , I saw you pinged the maintainers earlier regarding the |
I would just be guessing so I'll leave that to @yajo 🙇 |
|
I'd like to wait for @yajo's opinion, as he is the lead maintainer with a long history developing Copier. All of us are working on Copier as time permits. Let's just wait for him to find time to share his thoughts. |
Introduce the TaskError exception to handle subprocess failures gracefully. Modify `Worker` class in `copier/main.py` to use TaskError when a subprocess encounters a non-zero exit status. Update tests to expect TaskError instead of CalledProcessError.
Checked for all uses of UserMessageError to verify that it'll work. Also added __str__ method to UserMessageError.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1991 +/- ##
==========================================
- Coverage 98.13% 98.00% -0.13%
==========================================
Files 54 54
Lines 5832 5915 +83
==========================================
+ Hits 5723 5797 +74
- Misses 109 118 +9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
IIRC, |
|
Just to clarify your comment, @yajo: Is it a valid pattern to let any exception (like I guess the underlying question is: What is the meaning of |
|
@tsvikas Sorry for the delay. I've thought about the use of |
|
Thank you I've implemented printing of |
|
it seems that the codecov is complaining about those lines being untested, which is true, because any task used currently will not capture the stderr -- this is only a future facing change. what do you think we should do? |
|
Ah, I missed that
For our Python API users, That said, I suggest to revert/drop db1a6dc (and f29ce78) to keep this PR focused on introducing the |
|
done :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic, thanks for the great work, @tsvikas! 🙏
Introduce the TaskError exception to handle subprocess failures gracefully.
solves #1990