From 102ec3c26c9b49001bc866ad74c27c2c75e59b4f Mon Sep 17 00:00:00 2001 From: Ben White Date: Thu, 29 Aug 2024 09:25:41 +0200 Subject: [PATCH] feat: Throw if slack response is bad (#24646) --- posthog/cdp/templates/slack/template_slack.py | 2 +- posthog/cdp/templates/slack/test_template_slack.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/posthog/cdp/templates/slack/template_slack.py b/posthog/cdp/templates/slack/template_slack.py index 68d4f93274090..3158d82707958 100644 --- a/posthog/cdp/templates/slack/template_slack.py +++ b/posthog/cdp/templates/slack/template_slack.py @@ -23,7 +23,7 @@ }); if (res.status != 200 or not res.body.ok) { - print('Non-ok response:', res) + throw Error(f'Failed to post message to Slack: {res.status}: {res.body}'); } """.strip(), inputs_schema=[ diff --git a/posthog/cdp/templates/slack/test_template_slack.py b/posthog/cdp/templates/slack/test_template_slack.py index 9b8bab8cd1952..1060aa2441913 100644 --- a/posthog/cdp/templates/slack/test_template_slack.py +++ b/posthog/cdp/templates/slack/test_template_slack.py @@ -1,3 +1,5 @@ +import pytest +from hogvm.python.utils import UncaughtHogVMException from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest from posthog.cdp.templates.slack.template_slack import template as template_slack @@ -46,10 +48,13 @@ def test_function_works(self): def test_function_prints_warning_on_bad_status(self): self.mock_fetch_response = lambda *args: {"status": 400, "body": {"ok": True}} # type: ignore - self.run_function(self._inputs()) - assert self.get_mock_print_calls() == [("Non-ok response:", {"status": 400, "body": {"ok": True}})] + with pytest.raises(UncaughtHogVMException) as e: + self.run_function(self._inputs()) + + assert e.value.message == "Failed to post message to Slack: 400: {'ok': true}" def test_function_prints_warning_on_bad_body(self): self.mock_fetch_response = lambda *args: {"status": 200, "body": {"ok": False}} # type: ignore - self.run_function(self._inputs()) - assert self.get_mock_print_calls() == [("Non-ok response:", {"status": 200, "body": {"ok": False}})] + with pytest.raises(UncaughtHogVMException) as e: + self.run_function(self._inputs()) + assert e.value.message == "Failed to post message to Slack: 200: {'ok': false}"