core: harden message processing by catching wrong returns from v2 modules

This commit is contained in:
dymik739 2023-11-03 17:54:35 +02:00
parent e002dc46a2
commit a7bbb1c1cc
1 changed files with 19 additions and 1 deletions

20
main.py
View File

@ -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: