From 0b8a41a56a87f96b81d206acf771f548709576df Mon Sep 17 00:00:00 2001
From: anima
Date: Wed, 28 Oct 2026 22:17:11 +0100
Subject: [PATCH] extend error handling
---
main.py | 72 +++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 47 insertions(+), 25 deletions(-)
diff --git a/main.py b/main.py
index 0091e0e..0c7a7ed 100644
--- a/main.py
+++ b/main.py
@@ -62,31 +62,47 @@ class MatrixBot:
Returns:
bool: True if file was saved
"""
- session = {
- 'access_token': self.session.access_token,
- 'device_id': self.session.device_id,
- 'next_batch': self.client.next_batch
- }
- logging.debug('save session file')
- with open(self.session_file, 'w') as f:
- yaml.dump(session, f)
- return True
+ try:
+ session = {
+ 'access_token': self.session.access_token,
+ 'device_id': self.session.device_id,
+ 'next_batch': self.client.next_batch
+ }
+ logging.debug('save session file')
+ with open(self.session_file, 'w') as f:
+ yaml.dump(session, f)
+ return True
+ except Exception as e:
+ logging.error(f'Failed to save session to {self.session_file}: {str(e)}')
+ return False
def _load_session(self) -> bool:
- """Load session date from file.
+ """Load session data from file.
Returns:
bool: True if file was found and loaded
"""
if not os.path.exists(self.session_file):
+ logging.info(f'No session file found at {self.session_file}')
+ return False
+ try:
+ logging.info(f'restore session from file {self.session_file}')
+ with open(self.session_file, 'r') as f:
+ session = yaml.safe_load(f)
+
+ # Extended validation of session data
+ required_fields = ['access_token', 'device_id', 'next_batch']
+ if not all(field in session for field in required_fields):
+ logging.warning('Session file exists but is incomplete')
+ return False
+
+ self.client.access_token = session['access_token']
+ self.client.device_id = session['device_id']
+ self.client.next_batch = session['next_batch']
+ return True
+ except Exception as e:
+ logging.error(f'Failed to load session from {self.session_file}: {str(e)}')
return False
- logging.info(f'restore session from file {self.session_file}')
- with open(self.session_file, 'r') as f:
- session = yaml.safe_load(f)
- self.client.access_token = session['access_token']
- self.client.device_id = session['device_id']
- self.client.next_batch = session['next_batch']
- return True
async def _login(self) -> bool:
"""Restore login with session file or run full login process.
@@ -100,16 +116,22 @@ class MatrixBot:
logging.debug('restore session successfully')
return True
else:
- logging.warning('no session file was found or file is broken')
+ logging.warning('no session file was found or file is broken - will attempt full login')
logging.debug(f'try login with account {self.username}')
- self.session = await self.client.login(self.__password)
- if isinstance(self.session, LoginResponse):
- logging.info(f'login successfull with account {self.username} on server {self.homeserver}')
- logging.info(self.session)
- self._save_session()
- return True
- return False
+ try:
+ self.session = await self.client.login(self.__password)
+ if isinstance(self.session, LoginResponse):
+ logging.info(f'login successful with account {self.username} on server {self.homeserver}')
+ logging.info(self.session)
+ self._save_session()
+ return True
+ else:
+ logging.error(f"Login failed for user {self.username}: Unexpected response type {type(self.session)}")
+ return False
+ except Exception as e:
+ logging.error(f"Login attempt failed for user {self.username}: {str(e)}")
+ return False
def _find_skills(self) -> None:
"""Search all modules in skill folder and init these."""