Skip to content

Commit

Permalink
Suppress traceback on network disruption in Telegram app object
Browse files Browse the repository at this point in the history
Log error if network has disrupted, but do not print traceback
  • Loading branch information
aobolensk committed Apr 13, 2024
1 parent 2446c53 commit d4a4549
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/backend/telegram/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from telegram import Update, constants
from telegram.ext import Application, CallbackContext, MessageHandler, filters
from telegram.error import NetworkError

from src import const
from src.api.bot_instance import BotInstance
Expand Down Expand Up @@ -51,6 +52,15 @@ async def _handle_mentions(self, update: Update, context: CallbackContext) -> No
await bc.executor.commands[cmd_line[0]].run(cmd_line, TelegramExecutionContext(update, context))
await bc.plugin_manager.broadcast_command("on_message", TelegramExecutionContext(update, context))

@staticmethod
async def _error_handler(update: Update, context: CallbackContext) -> None:
# You can also log the error or do other error handling here
error = context.error
if isinstance(error, NetworkError):
log.error(f"Network error occurred: {error}")
return
raise error

@Mail.send_exception_info_to_admin_emails
def _run(self, args) -> None:
log.info("Starting Telegram instance...")
Expand All @@ -75,6 +85,7 @@ def _run(self, args) -> None:
bc.be.set_running(const.BotBackend.TELEGRAM, True, f"{bc.telegram.bot_username} ({self.__class__.__name__})")
bc.executor.binders[const.BotBackend.TELEGRAM] = TelegramCommandBinding(app)
bc.telegram.app = app
app.add_error_handler(self._error_handler)
app.run_polling(timeout=600, stop_signals=())
counter = 0
reminder_proc = ReminderProcessing()
Expand Down

0 comments on commit d4a4549

Please sign in to comment.