aboutsummaryrefslogtreecommitdiffstats
path: root/slack/error.py
blob: 8817759abe32642ac709ba1aa68a7d235947df50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from typing import TYPE_CHECKING, Dict, Mapping, Sequence, Union
from uuid import uuid4

from slack.shared import shared

if TYPE_CHECKING:
    from slack_api.slack_common import SlackErrorResponse

    from slack.slack_workspace import SlackWorkspace


class HttpError(Exception):
    def __init__(
        self,
        url: str,
        options: Dict[str, str],
        return_code: int,
        http_status_code: int,
        error: str,
    ):
        super().__init__(
            f"{self.__class__.__name__}: url='{url}', return_code={return_code}, http_status_code={http_status_code}, error='{error}'"
        )
        self.url = url
        self.options = options
        self.return_code = return_code
        self.http_status_code = http_status_code
        self.error = error


class SlackApiError(Exception):
    def __init__(
        self,
        workspace: SlackWorkspace,
        method: str,
        response: SlackErrorResponse,
        params: Mapping[
            str, Union[str, int, bool, Sequence[str], Sequence[int], Sequence[bool]]
        ] = {},
    ):
        super().__init__(
            f"{self.__class__.__name__}: workspace={workspace}, method='{method}', params={params}, response={response}"
        )
        self.workspace = workspace
        self.method = method
        self.params = params
        self.response = response


class SlackError(Exception):
    def __init__(self, workspace: SlackWorkspace, error: str):
        super().__init__(
            f"{self.__class__.__name__}: workspace={workspace}, error={error}"
        )
        self.workspace = workspace
        self.error = error


@dataclass
class UncaughtError:
    id: str = field(init=False)
    exception: BaseException

    def __post_init__(self):
        self.id = str(uuid4())
        self.time = datetime.now()


def store_and_format_exception(e: BaseException):
    uncaught_error = UncaughtError(e)
    shared.uncaught_errors.append(uncaught_error)
    stack_msg = f"(run /slack debug error {uncaught_error.id} for stack trace)"

    if isinstance(e, HttpError):
        return (
            f"Error calling URL {e.url}: return code: {e.return_code}, "
            f"http status code: {e.http_status_code}, error: {e.error} {stack_msg}"
        )
    elif isinstance(e, SlackApiError):
        return (
            f"Error from Slack API method {e.method} with params {e.params} for workspace "
            f"{e.workspace.name}: {e.response} {stack_msg}"
        )
    elif isinstance(e, SlackError):
        return f"Error occurred in workspace {e.workspace.name}: {e.error} {stack_msg}"
    else:
        return f"Unknown error occurred: {e.__class__.__name__}: {e} {stack_msg}"