Browse Source

OpsGenie integration returns more detailed error messages

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

+ 1
- 0
CHANGELOG.md View File

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


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

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


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

@ -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",


+ 1
- 1
hc/front/views.py View File

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


Loading…
Cancel
Save