Skip to content

Commit

Permalink
populate error to error_class mapping once
Browse files Browse the repository at this point in the history
  • Loading branch information
vstpme committed Sep 12, 2023
1 parent 828233d commit 5b1be85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
23 changes: 2 additions & 21 deletions engine/apps/slack/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
from slack_sdk.http_retry import HttpRequest, HttpResponse, RetryHandler, RetryState, default_retry_handlers
from slack_sdk.web import SlackResponse, WebClient

from apps.slack.errors import (
SlackAPIError,
SlackAPIRatelimitError,
SlackAPIServerError,
SlackAPITokenError,
UnexpectedResponse,
)
from apps.slack.errors import SlackAPIRatelimitError, SlackAPIServerError, SlackAPITokenError, get_error_class

if typing.TYPE_CHECKING:
from apps.slack.models import SlackTeamIdentity
Expand Down Expand Up @@ -138,7 +132,7 @@ def api_call(self, *args, **kwargs) -> SlackResponse:
)

# narrow down the error
error_class = self._get_error_class(e.response)
error_class = get_error_class(e.response)

# mark / unmark token as revoked
if error_class is SlackAPITokenError:
Expand All @@ -149,19 +143,6 @@ def api_call(self, *args, **kwargs) -> SlackResponse:
# raise the narrowed down error class
raise error_class(e.response) from e

@staticmethod
def _get_error_class(response: UnexpectedResponse | SlackResponse) -> typing.Type[SlackAPIError]:
"""Get an appropriate error class for the response"""

if isinstance(response, dict): # UnexpectedResponse
return SlackAPIServerError

for error_class in SlackAPIError.__subclasses__():
if response["error"] in error_class.errors:
return error_class

return SlackAPIError

def _mark_token_revoked(self) -> None:
if not self.slack_team_identity.detected_token_revoked:
self.slack_team_identity.detected_token_revoked = timezone.now()
Expand Down
16 changes: 15 additions & 1 deletion engine/apps/slack/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UnexpectedResponse(typing.TypedDict):
class SlackAPIError(Exception):
"""
Base class for Slack API errors. To add a new error class, add a new subclass of SlackAPIError in this file.
See apps.slack.client.SlackClient._get_error_class for more details on how these are raised.
See get_error_class at the end of this file for more details on how these are raised.
"""

errors: tuple[str, ...]
Expand Down Expand Up @@ -94,3 +94,17 @@ class SlackAPICannotDMBotError(SlackAPIError):

class SlackAPIMethodNotSupportedForChannelTypeError(SlackAPIError):
errors = ("method_not_supported_for_channel_type",)


_error_to_error_class = {
error: error_class for error_class in SlackAPIError.__subclasses__() for error in error_class.errors
}


def get_error_class(response: UnexpectedResponse | SlackResponse) -> typing.Type[SlackAPIError]:
"""Get an appropriate error class for the response"""

if isinstance(response, dict): # UnexpectedResponse
return SlackAPIServerError

return _error_to_error_class.get(response["error"], SlackAPIError)

0 comments on commit 5b1be85

Please sign in to comment.