Telegram integration returns more detailed error messages

This commit is contained in:
Pēteris Caune 2020-03-19 22:16:22 +02:00
parent 5f2c20e46b
commit 25d7d5409f
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
3 changed files with 29 additions and 14 deletions

View File

@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- Use Selectize.js for entering tags (#324) - Use Selectize.js for entering tags (#324)
- Zulip integration (#202) - Zulip integration (#202)
- OpsGenie integration returns more detailed error messages - OpsGenie integration returns more detailed error messages
- Telegram integration returns more detailed error messages
### Bug Fixes ### Bug Fixes
- The "render_docs" command checks if markdown and pygments is installed (#329) - The "render_docs" command checks if markdown and pygments is installed (#329)

View File

@ -592,6 +592,16 @@ class NotifyTestCase(BaseTestCase):
self.assertEqual(payload["chat_id"], 123) self.assertEqual(payload["chat_id"], 123)
self.assertTrue("The check" in payload["text"]) self.assertTrue("The check" in payload["text"])
@patch("hc.api.transports.requests.request")
def test_telegram_returns_error(self, mock_post):
self._setup_data("telegram", json.dumps({"id": 123}))
mock_post.return_value.status_code = 400
mock_post.return_value.json.return_value = {"description": "Hi"}
self.channel.notify(self.check)
n = Notification.objects.first()
self.assertEqual(n.error, 'Received status code 400 with a message: "Hi"')
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_sms(self, mock_post): def test_sms(self, mock_post):
self._setup_data("sms", "+1234567890") self._setup_data("sms", "+1234567890")

View File

@ -142,7 +142,9 @@ class Shell(Transport):
class HttpTransport(Transport): class HttpTransport(Transport):
@classmethod @classmethod
def get_error(cls, response): def get_error(cls, response):
return f"Received status code {response.status_code}" # Override in subclasses: look for a specific error message in the
# response and return it.
return None
@classmethod @classmethod
def _request(cls, method, url, **kwargs): def _request(cls, method, url, **kwargs):
@ -156,7 +158,12 @@ class HttpTransport(Transport):
r = requests.request(method, url, **options) r = requests.request(method, url, **options)
if r.status_code not in (200, 201, 202, 204): if r.status_code not in (200, 201, 202, 204):
return cls.get_error(r) m = cls.get_error(r)
if m:
return f'Received status code {r.status_code} with a message: "{m}"'
return f"Received status code {r.status_code}"
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
# Well, we tried # Well, we tried
return "Connection timed out" return "Connection timed out"
@ -261,15 +268,10 @@ class OpsGenie(HttpTransport):
@classmethod @classmethod
def get_error(cls, response): def get_error(cls, response):
try: try:
m = response.json().get("message") return response.json().get("message")
if m:
code = response.status_code
return f'Received status code {code} with a message: "{m}"'
except ValueError: except ValueError:
pass pass
return super().get_error(response)
def notify(self, check): def notify(self, check):
headers = { headers = {
"Conent-Type": "application/json", "Conent-Type": "application/json",
@ -441,6 +443,13 @@ class Discord(HttpTransport):
class Telegram(HttpTransport): class Telegram(HttpTransport):
SM = "https://api.telegram.org/bot%s/sendMessage" % settings.TELEGRAM_TOKEN SM = "https://api.telegram.org/bot%s/sendMessage" % settings.TELEGRAM_TOKEN
@classmethod
def get_error(cls, response):
try:
return response.json().get("description")
except ValueError:
pass
@classmethod @classmethod
def send(cls, chat_id, text): def send(cls, chat_id, text):
return cls.post( return cls.post(
@ -560,15 +569,10 @@ class Zulip(HttpTransport):
@classmethod @classmethod
def get_error(cls, response): def get_error(cls, response):
try: try:
m = response.json().get("msg") return response.json().get("msg")
if m:
code = response.status_code
return f'Received status code {code} with a message: "{m}"'
except ValueError: except ValueError:
pass pass
return super().get_error(response)
def notify(self, check): def notify(self, check):
_, domain = self.channel.zulip_bot_email.split("@") _, domain = self.channel.zulip_bot_email.split("@")
url = "https://%s/api/v1/messages" % domain url = "https://%s/api/v1/messages" % domain