diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index ca9b33c8..e38e5e1d 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -47,7 +47,9 @@ class Command(BaseCommand): tmpl = "\nSending alert, status=%s, code=%s\n" self.stdout.write(tmpl % (check.status, check.code)) - check.send_alert() + errors = check.send_alert() + for ch, error in errors: + self.stdout.write("ERROR: %s %s %s\n" % (ch.kind, ch.value, error)) connection.close() return True diff --git a/hc/api/models.py b/hc/api/models.py index 4f808fc4..02fc89ce 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -67,8 +67,13 @@ class Check(models.Model): if self.status not in ("up", "down"): raise NotImplementedError("Unexpected status: %s" % self.status) + errors = [] for channel in self.channel_set.all(): - channel.notify(self) + error = channel.notify(self) + if error not in ("", "no-op"): + errors.append((channel, error)) + + return errors def get_status(self): if self.status in ("new", "paused"): @@ -153,6 +158,8 @@ class Channel(models.Model): n.error = error n.save() + return error + def test(self): return self.transport().test() diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 121bd430..a3f1afe8 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -44,7 +44,7 @@ class NotifyTestCase(BaseTestCase): self.channel.notify(self.check) n = Notification.objects.get() - self.assertEqual(n.error, "A connection to http://example failed") + self.assertEqual(n.error, "Connection failed") @patch("hc.api.transports.requests.get") def test_webhooks_ignore_up_events(self, mock_get): diff --git a/hc/api/transports.py b/hc/api/transports.py index bf37099d..0e21d466 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -73,7 +73,7 @@ class Webhook(Transport): # Well, we tried return "Connection timed out" except requests.exceptions.ConnectionError: - return "A connection to %s failed" % self.channel.value + return "Connection failed" class JsonTransport(Transport): @@ -87,7 +87,7 @@ class JsonTransport(Transport): # Well, we tried return "Connection timed out" except requests.exceptions.ConnectionError: - return "A connection to %s failed" % url + return "Connection failed" class Slack(JsonTransport):