Skip to content

Commit

Permalink
Raise a CommandError instead of printing a message
Browse files Browse the repository at this point in the history
If an AnVILAPIError is raised by the command, re-raise a CommandError
instead of printing an error and exiting normally. This should give
the proper return code when the command is run, which means that
cron jobs encountering errors should email us.
  • Loading branch information
amstilp committed Nov 12, 2024
1 parent d3ebfe2 commit 985070b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import django_tables2 as tables
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import render_to_string

from ...anvil_api import AnVILAPIError
Expand Down Expand Up @@ -58,7 +58,7 @@ def _run_audit(self, audit_results, **options):
# Assume the method is called anvil_audit.
audit_results.run_audit()
except AnVILAPIError:
self.stdout.write(self.style.ERROR("API error."))
raise CommandError("API error.")
else:
if not audit_results.ok():
self.stdout.write(self.style.ERROR("problems found."))
Expand Down
4 changes: 2 additions & 2 deletions anvil_consortium_manager/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def test_command_run_audit_api_error(self):
api_url = self.get_api_url_billing_project(billing_project.name)
self.anvil_response_mock.add(responses.GET, api_url, status=500, json={"message": "error"})
out = StringIO()
call_command("run_anvil_audit", "--no-color", models=["BillingProject"], stdout=out)
self.assertIn("BillingProjectAudit... API error.", out.getvalue())
with self.assertRaises(CommandError):
call_command("run_anvil_audit", "--no-color", models=["BillingProject"], stdout=out)

# This test is complicated so skipping for now.
# When trying to change the settings, the test attempts to repopulate the
Expand Down

0 comments on commit 985070b

Please sign in to comment.