-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
✨ Add TaskError exception for subprocess management #1991
base: master
Are you sure you want to change the base?
Conversation
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.
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 |
Checked for all uses of UserMessageError to verify that it'll work. Also added __str__ method to UserMessageError.
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?
@@ -1,7 +1,11 @@ | |||
"""Custom exceptions used by Copier.""" | |||
|
|||
from __future__ import annotations |
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.
I'd prefer removing this line and reverting the typing changes accordingly, and address this change in a separate PR to avoid mixing concerns here.
personally i assumed that |
Introduce the TaskError exception to handle subprocess failures gracefully.
solves #1990