From 8c7f3977e21b32d94901e3f30af9bf43ca9c8c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 19 Mar 2020 21:58:17 +0200 Subject: [PATCH] OpsGenie integration returns more detailed error messages --- CHANGELOG.md | 1 + hc/api/tests/test_notify.py | 10 ++++++++++ hc/api/transports.py | 16 ++++++++++++++-- hc/front/views.py | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 933abdc3..8f26c842 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - API reference in Markdown - Use Selectize.js for entering tags (#324) - Zulip integration (#202) +- OpsGenie integration returns more detailed error messages ### Bug Fixes - The "render_docs" command checks if markdown and pygments is installed (#329) diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 8ae686ae..8fc872c1 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -502,6 +502,16 @@ class NotifyTestCase(BaseTestCase): args, kwargs = mock_post.call_args self.assertIn("api.eu.opsgenie.com", args[1]) + @patch("hc.api.transports.requests.request") + def test_opsgenie_returns_error(self, mock_post): + self._setup_data("opsgenie", "123") + mock_post.return_value.status_code = 403 + mock_post.return_value.json.return_value = {"message": "Nice try"} + + self.channel.notify(self.check) + n = Notification.objects.first() + self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"') + @patch("hc.api.transports.requests.request") def test_pushover(self, mock_post): self._setup_data("po", "123|0") diff --git a/hc/api/transports.py b/hc/api/transports.py index 39ef0e8f..b1729611 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -141,8 +141,8 @@ class Shell(Transport): class HttpTransport(Transport): @classmethod - def get_error(cls, r): - return "Received status code %d" % r.status_code + def get_error(cls, response): + return f"Received status code {response.status_code}" @classmethod def _request(cls, method, url, **kwargs): @@ -258,6 +258,18 @@ class HipChat(HttpTransport): class OpsGenie(HttpTransport): + @classmethod + def get_error(cls, response): + try: + m = response.json().get("message") + if m: + code = response.status_code + return f'Received status code {code} with a message: "{m}"' + except ValueError: + pass + + return super().get_error(response) + def notify(self, check): headers = { "Conent-Type": "application/json", diff --git a/hc/front/views.py b/hc/front/views.py index e247ee0a..22c1404d 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -775,7 +775,7 @@ def send_test_notification(request, code): error = channel.transport.notify(dummy) if error: - messages.warning(request, "Could not send a test notification: %s" % error) + messages.warning(request, "Could not send a test notification. %s" % error) else: messages.success(request, "Test notification sent!")