diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-02-02 21:53:40 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | a1c4b14c8f504b39cfadd78a9010c3ed6495e123 (patch) | |
tree | f4ad95fdf5eb6d05d27e9f1d36736048a5f4ee54 /slack/error.py | |
parent | b91385fe3d3538704deb06bb187dc48efa87e34b (diff) | |
download | wee-slack-a1c4b14c8f504b39cfadd78a9010c3ed6495e123.tar.gz |
Record uncaught errors and add command to display them
Diffstat (limited to 'slack/error.py')
-rw-r--r-- | slack/error.py | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/slack/error.py b/slack/error.py index a92626d..8817759 100644 --- a/slack/error.py +++ b/slack/error.py @@ -1,6 +1,11 @@ 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 @@ -55,18 +60,32 @@ class SlackError(Exception): self.error = error -def format_exception(e: BaseException): +@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}" + 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}" + f"{e.workspace.name}: {e.response} {stack_msg}" ) elif isinstance(e, SlackError): - return f"Error occurred in workspace {e.workspace.name}: {e.error}" + return f"Error occurred in workspace {e.workspace.name}: {e.error} {stack_msg}" else: - return f"Unknown error occurred: {e.__class__.__name__}: {e}" + return f"Unknown error occurred: {e.__class__.__name__}: {e} {stack_msg}" |