From c85d0d7debdc021a1a8c1472da5b264ed8f98400 Mon Sep 17 00:00:00 2001 From: Karl Mathias Moberg Date: Wed, 3 Apr 2024 01:10:22 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20debugging=20to=20database=20c?= =?UTF-8?q?all.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 7ac260c..4bdbf58 100644 --- a/main.py +++ b/main.py @@ -117,15 +117,41 @@ def updatePos(ts3conn): onlineController[position['identifier']] = [] # Get the user info for the controller from the ZNY website - userInfo = requests.get( - zny_web_instance + '/api/teamspeak/userIdentity?cid={}'.format(position['cid'])).json() + try: + user_info_response = requests.get( + zny_web_instance + '/api/teamspeak/userIdentity?cid={}'.format(position['cid'])) + user_info_response.raise_for_status() + if not user_info_response.content: + raise ValueError("Empty response received from user info API.") + userInfo = user_info_response.json() + except requests.RequestException as e: + logger.error(f"Request failed: {e}") + raise + except ValueError as e: + logger.error(f"Invalid response: {e}") + raise + except json.JSONDecodeError as e: + logger.error(f"Failed to parse JSON response: {e}") + logger.error(f"Response content: {user_info_response.text}") + raise + # Add the user to the position in the dictionary for that position for uid in userInfo: onlineController[position['identifier']].append(uid) # Connect to the database - conn = engine.connect() + try: + conn = engine.connect() + except Exception as e: + # Log the error with as much detail as possible + logger.error(f"Database connection failed: {e}") + # It might be beneficial to include the database URL, masking sensitive info + sanitized_db_url = re.sub(r'//(.*):(.*)@', '//***:***@', str(db)) + logger.error(f"Failed to connect to database at {sanitized_db_url}") + # After logging, you might want to raise an exception to halt the execution + # or handle the error in a way that makes sense for your application + raise # Get the list of all positions from the database positionsAll = conn.execute(select([table])).fetchall()