From 8f3a5bdc1ec969ae069451ddf8c68ade002782bc Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sun, 29 Jan 2023 00:19:17 +0100 Subject: Improve error printing --- slack/error.py | 23 +++++++++++++++++++++-- slack/http.py | 2 +- slack/log.py | 3 ++- slack/slack_workspace.py | 3 +++ slack/task.py | 21 ++++++++++----------- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/slack/error.py b/slack/error.py index d69fbf9..1d9a975 100644 --- a/slack/error.py +++ b/slack/error.py @@ -17,7 +17,9 @@ class HttpError(Exception): http_status_code: int, error: str, ): - super().__init__() + 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 @@ -35,8 +37,25 @@ class SlackApiError(Exception): str, Union[str, int, bool, Sequence[str], Sequence[int], Sequence[bool]] ] = {}, ): - super().__init__() + 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 + + +def format_exception(e: BaseException): + 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}" + ) + 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}" + ) + else: + return f"Unknown error occurred: {e.__class__.__name__}: {e}" diff --git a/slack/http.py b/slack/http.py index b899690..9347842 100644 --- a/slack/http.py +++ b/slack/http.py @@ -46,7 +46,7 @@ async def hook_process_hashtable(command: str, options: Dict[str, str], timeout: stderr.write(err) out = stdout.getvalue() - err = stderr.getvalue() + err = stderr.getvalue().strip() log( LogLevel.DEBUG, f"hook_process_hashtable response ({future.id}): command: {command}, " diff --git a/slack/log.py b/slack/log.py index 552727d..e33b744 100644 --- a/slack/log.py +++ b/slack/log.py @@ -23,4 +23,5 @@ def print_error(message: str): def log(level: LogLevel, message: str): if level >= LogLevel.INFO: - print(level, message) + prefix = weechat.prefix("error") if level >= LogLevel.ERROR else "\t" + weechat.prnt("", f"{prefix}{shared.SCRIPT_NAME} {level.name}: {message}") diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 217becf..c5c4732 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -127,6 +127,9 @@ class SlackWorkspace: self.usergroups = SlackUsergroups(self) self.conversations: Dict[str, SlackConversation] = {} + def __repr__(self): + return f"{self.__class__.__name__}('{self.name}')" + @property def is_connected(self): return self._is_connected diff --git a/slack/task.py b/slack/task.py index d765811..2f68181 100644 --- a/slack/task.py +++ b/slack/task.py @@ -20,7 +20,7 @@ from uuid import uuid4 import weechat -from slack.error import HttpError, SlackApiError +from slack.error import HttpError, SlackApiError, format_exception from slack.log import print_error from slack.shared import shared from slack.util import get_callback_name @@ -40,6 +40,9 @@ class Future(Awaitable[T]): self._finished = False self._result: Optional[T] = None + def __repr__(self) -> str: + return f"{self.__class__.__name__}('{self.id}')" + def __await__(self) -> Generator[Future[T], T, T]: result = yield self if isinstance(result, BaseException): @@ -73,6 +76,9 @@ class Task(Future[T]): super().__init__() self.coroutine = coroutine + def __repr__(self) -> str: + return f"{self.__class__.__name__}('{self.id}', coroutine={self.coroutine.__qualname__})" + def weechat_task_cb(data: str, *args: Any) -> int: future = shared.active_futures.pop(data) @@ -114,16 +120,9 @@ def task_runner(task: Task[Any], response: Any): or not weechat_task_cb_in_stack and create_task_in_stack == 1 ): - if isinstance(e, HttpError): - print_error( - f"Error calling URL {e.url}: return code: {e.return_code}, " - f"http status code: {e.http_status_code}, error: {e.error}, task: {task}" - ) - elif isinstance(e, SlackApiError): - print_error( - f"Error from Slack API method {e.method} with params {e.params} for workspace " - f"{e.workspace.name}: {e.response}, task: {task}" - ) + if isinstance(e, HttpError) or isinstance(e, SlackApiError): + exception_str = format_exception(e) + print_error(f"{exception_str}, task: {task}") else: raise e -- cgit