From e8910971e455ad219b25c11f671bfc1314cf04ff Mon Sep 17 00:00:00 2001 From: Anton Date: Sat, 27 Jan 2024 22:47:21 +0500 Subject: [PATCH] impr: shorten error messages (build 189) --- bot/bot.py | 8 +++++--- bot/commands/error.py | 3 ++- bot/config.py | 2 +- tests/test_commands.py | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 6441ab6..a8aa1d6 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -2,6 +2,7 @@ import logging import sys +import textwrap import time from telegram import Chat, Message @@ -157,9 +158,10 @@ async def reply_to(message: Message, context: CallbackContext, question: str) -> except Exception as exc: class_name = f"{exc.__class__.__module__}.{exc.__class__.__qualname__}" - error_text = f"Failed to answer. Reason: {class_name}: {exc}" - logger.error(error_text) - await message.reply_text(error_text) + error_text = f"{class_name}: {exc}" + logger.error("Failed to answer: %s", error_text) + text = textwrap.shorten(f"⚠️ {error_text}", width=255, placeholder="...") + await message.reply_text(text) async def _ask_question( diff --git a/bot/commands/error.py b/bot/commands/error.py index 07a3253..8a0386c 100644 --- a/bot/commands/error.py +++ b/bot/commands/error.py @@ -1,6 +1,7 @@ """Generic error handler.""" import logging +import textwrap from telegram import Chat, Update from telegram.ext import CallbackContext @@ -20,7 +21,7 @@ async def __call__(self, update: Update, context: CallbackContext) -> None: class_name = f"{context.error.__class__.__module__}.{context.error.__class__.__qualname__}" error_text = f"{class_name}: {context.error}" logger.warning("Exception while handling an update %s: %s", update, error_text) - text = f"⚠️ {context.error}" + text = textwrap.shorten(f"⚠️ {error_text}", width=255, placeholder="...") message = update.message reply_to_message_id = message.id if message and message.chat.type != Chat.PRIVATE else None diff --git a/bot/config.py b/bot/config.py index b43aa66..ffb4707 100644 --- a/bot/config.py +++ b/bot/config.py @@ -89,7 +89,7 @@ class Config: # Config schema version. Increments for backward-incompatible changes. schema_version = 4 # Bot version. - version = 188 + version = 189 def __init__(self, filename: str, src: dict) -> None: # Config filename. diff --git a/tests/test_commands.py b/tests/test_commands.py index dbfe1f0..bc8d935 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -380,7 +380,7 @@ async def test_exception(self): askers.TextAsker.model = FakeGPT(error=Exception("connection timeout")) update = self._create_update(11, text="What is your name?") await self.command(update, self.context) - self.assertTrue(self.bot.text.startswith("Failed to answer")) + self.assertTrue(self.bot.text.startswith("⚠️ builtins.Exception:")) self.assertTrue("connection timeout" in self.bot.text) @@ -498,4 +498,4 @@ async def test_error(self): update = self._create_update(11, "Something went wrong") update._effective_chat = self.chat await command(update, self.context) - self.assertEqual(self.bot.text, "⚠️ Something went wrong") + self.assertEqual(self.bot.text, "⚠️ builtins.Exception: Something went wrong")