From a7bbb1c1cc2510efc7d22970566e2ae10bb5d335 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Fri, 3 Nov 2023 17:54:35 +0200 Subject: [PATCH] core: harden message processing by catching wrong returns from v2 modules --- main.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 2e7a080..f4e15c7 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ import traceback # global variables STOP_REQUESTED = False +DEBUG_MODE = False DELAY_AFTER_RESPONSE = 3 DELAY_AFTER_MESSAGE = 0.1 DELAY_AFTER_IDLE = 1.0 @@ -48,6 +49,8 @@ class ModuleV1: def set_predefine(self): try: + if DEBUG_MODE: + print(f"Predefine on module v1 {self.alias} ({self.path})") exec(self.predefine) except Exception as e: print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\" " @@ -59,6 +62,8 @@ class ModuleV1: self.MESSAGE = msg try: + if DEBUG_MODE: + print(f"Calling module v1 {self.alias} ({self.path})") exec(self.code) return self.RESPONSE, self.FORMAT except Exception as e: @@ -79,6 +84,8 @@ class ModuleV2: # running the module def process(self, msg): try: + if DEBUG_MODE: + print(f"Calling module v2 {self.alias} ({self.path})") return self.obj.process(msg, self.path) except Exception as e: print(f"[ERROR] module v2: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") @@ -276,13 +283,24 @@ def queue_processor(): print(f"[INFO]: Queue length is {len(message_queue)}") updater.bot.send_message(msg.chat.id, f"[INFO]: Queue length is {len(message_queue)}") + elif len(command) == 2 and command[0] == "debug": + global DEBUG_MODE + if command[1] == "on": + DEBUG_MODE = True + else: + DEBUG_MODE = False + continue # modules are used in here for mod in mcu.modules: if mod.enabled: if mod.version in [1, 2]: - response, formatting = mod.process(msg) + try: + response, formatting = mod.process(msg) + except Exception as e: + print(f"Module {mod.alias} ({mod.path}) failed to do a proper return, skipping...") + continue if response: if not formatting: