From 985070b2cf05f45d333fd52b3f338f553e9f4af7 Mon Sep 17 00:00:00 2001 From: Adrienne Stilp Date: Tue, 12 Nov 2024 14:38:03 -0800 Subject: [PATCH] Raise a CommandError instead of printing a message 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. --- .../management/commands/run_anvil_audit.py | 4 ++-- anvil_consortium_manager/tests/test_commands.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/anvil_consortium_manager/management/commands/run_anvil_audit.py b/anvil_consortium_manager/management/commands/run_anvil_audit.py index 261d0e11..1832abaf 100644 --- a/anvil_consortium_manager/management/commands/run_anvil_audit.py +++ b/anvil_consortium_manager/management/commands/run_anvil_audit.py @@ -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 @@ -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.")) diff --git a/anvil_consortium_manager/tests/test_commands.py b/anvil_consortium_manager/tests/test_commands.py index e1fc11b0..8ce9a3ca 100644 --- a/anvil_consortium_manager/tests/test_commands.py +++ b/anvil_consortium_manager/tests/test_commands.py @@ -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