Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling when updating non-existing record #306

Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion tardis/plugins/auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ 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:
if str(e).startswith(
"Reqwest Error: HTTP status client error (400 Bad Request)"
):
self.logger.debug(
f"Could not update record {record.record_id}, "
"it probably does not exist in the database"
)
else:
raise

def construct_record(self, resource_attributes: AttributeDict):
"""
Expand Down
40 changes: 40 additions & 0 deletions tests/plugins_t/test_auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,46 @@ 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 = RuntimeError("Does not match RegEx")
with self.assertRaises(RuntimeError):
run_async(
self.plugin.notify,
state=DownState(),
resource_attributes=self.test_param,
)

self.client.update.side_effect = ValueError("Other exception")
with self.assertRaises(ValueError):
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)

Expand Down