From f02066d4edf0b047453d2abf7cb161a62c042b88 Mon Sep 17 00:00:00 2001 From: Michael Ansel Date: Fri, 14 Nov 2025 16:07:43 +0000 Subject: [PATCH] fix: escape markdown in error messages for Telegram Error messages containing JSON or special markdown characters break Telegram's markdown parser with "Can't parse entities" errors. Changes: - Escape special markdown characters (_, *, `, [) in error messages - Truncate very long error messages to 200 characters - Prevents Telegram parse errors while preserving error visibility Fixes Telegram markdown parsing errors when displaying error messages. --- src/bot/handlers/message.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bot/handlers/message.py b/src/bot/handlers/message.py index d2fc497..92c6584 100644 --- a/src/bot/handlers/message.py +++ b/src/bot/handlers/message.py @@ -120,9 +120,16 @@ def _format_error_message(error_str: str) -> str: ) else: # Generic error handling + # Escape special markdown characters in error message + # Replace problematic chars that break Telegram markdown + safe_error = error_str.replace("_", "\\_").replace("*", "\\*").replace("`", "\\`").replace("[", "\\[") + # Truncate very long errors + if len(safe_error) > 200: + safe_error = safe_error[:200] + "..." + return ( f"❌ **Claude Code Error**\n\n" - f"Failed to process your request: {error_str}\n\n" + f"Failed to process your request: {safe_error}\n\n" f"Please try again or contact the administrator if the problem persists." )