From 5a80fe189d9b0e0897317716a9450dfb8c046f59 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sun, 14 Dec 2025 23:49:10 +0800 Subject: [PATCH] Add error handling to quoted-printable decoding Wrapped the decode_quoted_printable function in a try-except block to handle decoding errors gracefully. If decoding fails, a warning is logged and the original value is returned, improving robustness when processing malformed vCard data. --- Whatsapp_Chat_Exporter/vcards_contacts.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Whatsapp_Chat_Exporter/vcards_contacts.py b/Whatsapp_Chat_Exporter/vcards_contacts.py index 4cb285d..03d60ce 100644 --- a/Whatsapp_Chat_Exporter/vcards_contacts.py +++ b/Whatsapp_Chat_Exporter/vcards_contacts.py @@ -40,8 +40,16 @@ class ContactsFromVCards: def decode_quoted_printable(value: str, charset: str) -> str: """Decode a vCard value that may be quoted-printable UTF-8.""" - bytes_val = quopri.decodestring(value) - return bytes_val.decode(charset, errors="replace") + try: + bytes_val = quopri.decodestring(value) + return bytes_val.decode(charset, errors="replace") + except Exception: + # Fallback: return the original value if decoding fails + logger.warning( + f"Failed to decode quoted-printable value: {value}, " + f"charset: {charset}. Please report this issue.{CLEAR_LINE}" + ) + return value def _parse_vcard_line(line: str) -> tuple[str, dict[str, str], str] | None: """ @@ -143,10 +151,7 @@ def process_vcard_entry(entry: str) -> dict | bool: if not name: return False - # 2. Extract phone numbers numbers = get_vcard_value(entry, "TEL") - - # Ensure at least one number was found if not numbers: return False