Zulip integration returns more detailed error messages

This commit is contained in:
Pēteris Caune 2020-03-19 22:05:13 +02:00
parent 8c7f3977e2
commit 5f2c20e46b
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
2 changed files with 23 additions and 5 deletions

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": "bot@example.org",
"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"')

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("@")