Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion computedfields/management/commands/checkdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def handle(self, *app_labels, **options):
end_time = time()
duration = int(end_time - start_time)
self.eprint(f'\nTotal check time: {timedelta(seconds=duration)}')
sys.exit(1 if has_desync else 0)
if has_desync:
raise CommandError('Desync data found.', returncode=1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does that CommandError behave in call_command? Will it raise there or just return the non 0 returncode? (Not sure atm, if call_command uses a subprocess...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I missed your question. I think call_command does nothing fancy so it will just bubble up as documented here:

CommandError(returncode=1)

Exception class indicating a problem while executing a management command.

If this exception is raised during the execution of a management command from a command line console, it will be caught and turned into a nicely-printed error message to the appropriate output stream (i.e., stderr); as a result, raising this exception (with a sensible description of the error) is the preferred way to indicate that something has gone wrong in the execution of a command. It accepts the optional returncode argument to customize the exit status for the management command to exit with, using sys.exit().

If a management command is called from code through call_command(), it’s up to you to catch the exception when needed.

It's true that for a command that explicitly does a check, throwing an exception is a bit counter intuitive, as nothing has really gone wrong... but it's inherent of management commands being a CLI. We could factor the logic out of the management command and expose it in the python API, where it could return a boolean. Would you prefer that ?


@transaction.atomic
def action_check(self, models, progress, size, json_out):
Expand Down