Skip to content

Commit c85d0d7

Browse files
committed
✨ Add debugging to database call.
1 parent 2b2d6cc commit c85d0d7

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

main.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,41 @@ def updatePos(ts3conn):
117117
onlineController[position['identifier']] = []
118118

119119
# Get the user info for the controller from the ZNY website
120-
userInfo = requests.get(
121-
zny_web_instance + '/api/teamspeak/userIdentity?cid={}'.format(position['cid'])).json()
120+
try:
121+
user_info_response = requests.get(
122+
zny_web_instance + '/api/teamspeak/userIdentity?cid={}'.format(position['cid']))
123+
user_info_response.raise_for_status()
124+
if not user_info_response.content:
125+
raise ValueError("Empty response received from user info API.")
126+
userInfo = user_info_response.json()
127+
except requests.RequestException as e:
128+
logger.error(f"Request failed: {e}")
129+
raise
130+
except ValueError as e:
131+
logger.error(f"Invalid response: {e}")
132+
raise
133+
except json.JSONDecodeError as e:
134+
logger.error(f"Failed to parse JSON response: {e}")
135+
logger.error(f"Response content: {user_info_response.text}")
136+
raise
137+
122138

123139
# Add the user to the position in the dictionary for that position
124140
for uid in userInfo:
125141
onlineController[position['identifier']].append(uid)
126142

127143
# Connect to the database
128-
conn = engine.connect()
144+
try:
145+
conn = engine.connect()
146+
except Exception as e:
147+
# Log the error with as much detail as possible
148+
logger.error(f"Database connection failed: {e}")
149+
# It might be beneficial to include the database URL, masking sensitive info
150+
sanitized_db_url = re.sub(r'//(.*):(.*)@', '//***:***@', str(db))
151+
logger.error(f"Failed to connect to database at {sanitized_db_url}")
152+
# After logging, you might want to raise an exception to halt the execution
153+
# or handle the error in a way that makes sense for your application
154+
raise
129155

130156
# Get the list of all positions from the database
131157
positionsAll = conn.execute(select([table])).fetchall()

0 commit comments

Comments
 (0)