Skip to content

Commit 385cb68

Browse files
committed
try/except patch
1 parent 4f50499 commit 385cb68

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/text_message_handler.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55

66
import re
7+
import html
78
import configparser
89
import os
910
import sys
@@ -714,7 +715,14 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
714715
# View the output (i.e. for markdown etc formatting debugging)
715716
logger.info(f"[Debug] Reply message before escaping: {bot_reply}")
716717

717-
escaped_reply = markdown_to_html(bot_reply)
718+
# escaped_reply = markdown_to_html(bot_reply)
719+
720+
try:
721+
escaped_reply = markdown_to_html(bot_reply)
722+
except Exception as e:
723+
bot.logger.error(f"markdown_to_html failed: {e}")
724+
escaped_reply = html.escape(bot_reply) # Safe fallback
725+
718726
# escaped_reply = bot_reply
719727
logger.info(f"[Debug] Reply message after escaping: {escaped_reply}")
720728

@@ -788,7 +796,12 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
788796
# return content
789797

790798
# Convert markdown to HTML
791-
escaped_reply = markdown_to_html(bot_reply)
799+
# escaped_reply = markdown_to_html(bot_reply)
800+
try:
801+
escaped_reply = markdown_to_html(bot_reply)
802+
except Exception as e:
803+
bot.logger.error(f"markdown_to_html failed: {e}")
804+
escaped_reply = html.escape(bot_reply) # Safe fallback
792805

793806
# Sanitize the HTML to remove any unsupported tags
794807
escaped_reply = sanitize_html(escaped_reply)
@@ -860,7 +873,13 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
860873
# return content
861874

862875
# Convert markdown to HTML
863-
escaped_reply = markdown_to_html(bot_reply)
876+
# escaped_reply = markdown_to_html(bot_reply)
877+
878+
try:
879+
escaped_reply = markdown_to_html(bot_reply)
880+
except Exception as e:
881+
bot.logger.error(f"markdown_to_html failed: {e}")
882+
escaped_reply = html.escape(bot_reply) # Safe fallback
864883

865884
# Sanitize the HTML to remove any unsupported tags
866885
escaped_reply = sanitize_html(escaped_reply)
@@ -946,7 +965,13 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
946965
# View the output (i.e. for markdown etc formatting debugging)
947966
logger.info(f"[Debug] Reply message before escaping: {bot_reply}")
948967

949-
escaped_reply = markdown_to_html(bot_reply)
968+
# escaped_reply = markdown_to_html(bot_reply)
969+
try:
970+
escaped_reply = markdown_to_html(bot_reply)
971+
except Exception as e:
972+
bot.logger.error(f"markdown_to_html failed: {e}")
973+
escaped_reply = html.escape(bot_reply) # Safe fallback
974+
950975
logger.info(f"[Debug] Reply message after escaping: {escaped_reply}")
951976

952977
# Log the bot's response
@@ -1116,7 +1141,13 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
11161141
# Ensure the bot has a substantive response to send
11171142
if bot_reply:
11181143
# escaped_reply = bot_reply
1119-
escaped_reply = markdown_to_html(bot_reply)
1144+
# escaped_reply = markdown_to_html(bot_reply)
1145+
1146+
try:
1147+
escaped_reply = markdown_to_html(bot_reply)
1148+
except Exception as e:
1149+
bot.logger.error(f"markdown_to_html failed: {e}")
1150+
escaped_reply = html.escape(bot_reply) # Safe fallback
11201151

11211152
# Log the bot's response from Perplexity API
11221153
bot.log_message(
@@ -1300,7 +1331,13 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
13001331
# view the output (i.e. for markdown etc formatting debugging)
13011332
logger.info(f"[Debug] Reply message before escaping: {bot_reply}")
13021333

1303-
escaped_reply = markdown_to_html(bot_reply)
1334+
# escaped_reply = markdown_to_html(bot_reply)
1335+
try:
1336+
escaped_reply = markdown_to_html(bot_reply)
1337+
except Exception as e:
1338+
bot.logger.error(f"markdown_to_html failed: {e}")
1339+
escaped_reply = html.escape(bot_reply) # Safe fallback
1340+
13041341
# escaped_reply = bot_reply
13051342
logger.info(f"[Debug] Reply message after escaping: {escaped_reply}")
13061343

0 commit comments

Comments
 (0)