diff --git a/tardis/plugins/auditor.py b/tardis/plugins/auditor.py index b2d120e4..98cfcff2 100644 --- a/tardis/plugins/auditor.py +++ b/tardis/plugins/auditor.py @@ -8,9 +8,13 @@ import logging import pytz +import re from tzlocal import get_localzone +REQWEST_ERROR = re.compile(r"Reqwest Error: HTTP status client error \((\d{3}).*\).*") + + class Auditor(Plugin): """ The :py:class:`~tardis.plugins.auditor.Auditor` plugin is a collector for the @@ -85,7 +89,21 @@ async def notify(self, state: State, resource_attributes: AttributeDict) -> None .replace(tzinfo=self._local_timezone) .astimezone(pytz.utc) ) - await self._client.update(record) + try: + await self._client.update(record) + except RuntimeError as e: + reqwest_error_match = REQWEST_ERROR.match(str(e)) + if reqwest_error_match: + http_error_code = reqwest_error_match.group(1) + if http_error_code == "400": + self.logger.debug( + f"Could not update record {record.record_id}, " + "it probably does not exist in the database" + ) + else: + raise + else: + raise def construct_record(self, resource_attributes: AttributeDict): """ diff --git a/tests/plugins_t/test_auditor.py b/tests/plugins_t/test_auditor.py index b1c7ce5a..09cc6e4c 100644 --- a/tests/plugins_t/test_auditor.py +++ b/tests/plugins_t/test_auditor.py @@ -143,6 +143,38 @@ def test_notify(self): self.assertEqual(self.client.add.call_count, 1) self.assertEqual(self.client.update.call_count, 1) + # test exception handling + self.client.update.side_effect = RuntimeError( + "Reqwest Error: HTTP status client error (400 Bad Request) " + "for url (http://127.0.0.1:8000/update)" + ) + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = RuntimeError( + "Reqwest Error: HTTP status client error (403 Forbidden) " + "for url (http://127.0.0.1:8000/update)" + ) + with self.assertRaises(RuntimeError): + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = Exception("Other exception") + with self.assertRaises(Exception): + run_async( + self.plugin.notify, + state=DownState(), + resource_attributes=self.test_param, + ) + + self.client.update.side_effect = None + def test_construct_record(self): record = self.plugin.construct_record(resource_attributes=self.test_param)