Browse Source

Zulip integration returns more detailed error messages

pull/344/head
Pēteris Caune 5 years ago
parent
commit
5f2c20e46b
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 23 additions and 5 deletions
  1. +17
    -0
      hc/api/tests/test_notify.py
  2. +6
    -5
      hc/api/transports.py

+ 17
- 0
hc/api/tests/test_notify.py View File

@ -826,3 +826,20 @@ class NotifyTestCase(BaseTestCase):
args, kwargs = mock_post.call_args
payload = kwargs["data"]
self.assertIn("DOWN", payload["topic"])
@patch("hc.api.transports.requests.request")
def test_zulip_returns_error(self, mock_post):
definition = {
"bot_email": "[email protected]",
"api_key": "fake-key",
"mtype": "stream",
"to": "general",
}
self._setup_data("zulip", json.dumps(definition))
mock_post.return_value.status_code = 403
mock_post.return_value.json.return_value = {"msg": "Nice try"}
self.channel.notify(self.check)
n = Notification.objects.first()
self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')

+ 6
- 5
hc/api/transports.py View File

@ -558,15 +558,16 @@ class MsTeams(HttpTransport):
class Zulip(HttpTransport):
@classmethod
def get_error(cls, r):
def get_error(cls, response):
try:
doc = r.json()
if "msg" in doc:
return doc["msg"]
m = response.json().get("msg")
if m:
code = response.status_code
return f'Received status code {code} with a message: "{m}"'
except ValueError:
pass
return super().get_error(r)
return super().get_error(response)
def notify(self, check):
_, domain = self.channel.zulip_bot_email.split("@")


Loading…
Cancel
Save