Просмотр исходного кода

ref(slack plugin): Don't raise errors for unactionable things (#30998)

* ref(slack plugin): Don't raise errors for unactionable things
Colleen O'Rourke 3 лет назад
Родитель
Сommit
2a7a0aac94
2 измененных файлов с 36 добавлено и 3 удалено
  1. 11 3
      src/sentry_plugins/slack/client.py
  2. 25 0
      tests/sentry_plugins/slack/test_plugin.py

+ 11 - 3
src/sentry_plugins/slack/client.py

@@ -1,6 +1,13 @@
 from sentry.shared_integrations.exceptions import ApiError
 from sentry_plugins.client import ApiClient
 
+IGNORABLE_SLACK_ERRORS = [
+    "channel_is_archived",
+    "invalid_channel",
+    "invalid_token",
+    "action_prohibited",
+]
+
 
 class SlackApiClient(ApiClient):
     plugin_name = "slack"
@@ -19,6 +26,7 @@ class SlackApiClient(ApiClient):
                 path=self.webhook, method="post", data=data, json=False, allow_text=True
             )
         except ApiError as e:
-            # Ignore 404 from slack webhooks
-            if e.code != 404:
-                raise e
+            # Ignore 404 and ignorable errors from slack webhooks
+            if e.text and e.text in IGNORABLE_SLACK_ERRORS or e.code == 404:
+                return
+            raise e

+ 25 - 0
tests/sentry_plugins/slack/test_plugin.py

@@ -154,3 +154,28 @@ class SlackPluginTest(PluginTestCase):
         with self.options({"system.url-prefix": "http://example.com"}):
             with pytest.raises(ApiError):
                 self.plugin.notify(notification)
+
+    @responses.activate
+    def test_no_error_on_ignorable_slack_errors(self):
+        responses.add("POST", "http://example.com/slack", status=403, body="action_prohibited")
+        self.plugin.set_option("webhook", "http://example.com/slack", self.project)
+
+        event = self.store_event(
+            data={"message": "Hello world", "level": "warning", "culprit": "foo.bar"},
+            project_id=self.project.id,
+        )
+
+        rule = Rule.objects.create(project=self.project, label="my rule")
+
+        notification = Notification(event=event, rule=rule)
+
+        # No exception since certain errors are supposed to be ignored
+        with self.options({"system.url-prefix": "http://example.com"}):
+            self.plugin.notify(notification)
+
+        responses.replace("POST", "http://example.com/slack", status=403, body="some_other_error")
+
+        # Other exceptions should not be ignored
+        with self.options({"system.url-prefix": "http://example.com"}):
+            with pytest.raises(ApiError):
+                self.plugin.notify(notification)