Browse Source

Telegram integration returns more detailed error messages

pull/344/head
Pēteris Caune 5 years ago
parent
commit
25d7d5409f
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 29 additions and 14 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +10
    -0
      hc/api/tests/test_notify.py
  3. +18
    -14
      hc/api/transports.py

+ 1
- 0
CHANGELOG.md 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)
- Zulip integration (#202)
- OpsGenie integration returns more detailed error messages
- Telegram integration returns more detailed error messages
### Bug Fixes
- The "render_docs" command checks if markdown and pygments is installed (#329)


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

@ -592,6 +592,16 @@ class NotifyTestCase(BaseTestCase):
self.assertEqual(payload["chat_id"], 123)
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")
def test_sms(self, mock_post):
self._setup_data("sms", "+1234567890")


+ 18
- 14
hc/api/transports.py View File

@ -142,7 +142,9 @@ class Shell(Transport):
class HttpTransport(Transport):
@classmethod
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
def _request(cls, method, url, **kwargs):
@ -156,7 +158,12 @@ class HttpTransport(Transport):
r = requests.request(method, url, **options)
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:
# Well, we tried
return "Connection timed out"
@ -261,15 +268,10 @@ 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}"'
return response.json().get("message")
except ValueError:
pass
return super().get_error(response)
def notify(self, check):
headers = {
"Conent-Type": "application/json",
@ -441,6 +443,13 @@ class Discord(HttpTransport):
class Telegram(HttpTransport):
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
def send(cls, chat_id, text):
return cls.post(
@ -560,15 +569,10 @@ class Zulip(HttpTransport):
@classmethod
def get_error(cls, response):
try:
m = response.json().get("msg")
if m:
code = response.status_code
return f'Received status code {code} with a message: "{m}"'
return response.json().get("msg")
except ValueError:
pass
return super().get_error(response)
def notify(self, check):
_, domain = self.channel.zulip_bot_email.split("@")
url = "https://%s/api/v1/messages" % domain


Loading…
Cancel
Save