Skip to content

Commit

Permalink
impr: shorten error messages (build 189)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Jan 27, 2024
1 parent 69f5a13 commit e891097
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import sys
import textwrap
import time

from telegram import Chat, Message
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion bot/commands/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Generic error handler."""

import logging
import textwrap
from telegram import Chat, Update
from telegram.ext import CallbackContext

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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")

0 comments on commit e891097

Please sign in to comment.