fix(logger): handle empty/corrupt member log files gracefully
Return None instead of crashing when check_previous_roles finds no parsable JSON lines in a member's log file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b3db1ef9f5
commit
f570ac4fce
1 changed files with 9 additions and 5 deletions
|
|
@ -107,17 +107,17 @@ class AdvancedLogger:
|
||||||
def check_previous_roles(self, member_id: int) -> Optional[Dict]:
|
def check_previous_roles(self, member_id: int) -> Optional[Dict]:
|
||||||
"""Check if member has previous role data when rejoining"""
|
"""Check if member has previous role data when rejoining"""
|
||||||
member_log_file = f"{self.member_logs_dir}/{member_id}.json"
|
member_log_file = f"{self.member_logs_dir}/{member_id}.json"
|
||||||
|
|
||||||
if not os.path.exists(member_log_file):
|
if not os.path.exists(member_log_file):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logs = []
|
logs = []
|
||||||
with open(member_log_file, 'r', encoding='utf-8') as f:
|
with open(member_log_file, 'r', encoding='utf-8') as f:
|
||||||
content = f.read().strip()
|
content = f.read().strip()
|
||||||
if not content:
|
if not content:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Handle legacy JSON list format
|
# Handle legacy JSON list format
|
||||||
if content.startswith('['):
|
if content.startswith('['):
|
||||||
try:
|
try:
|
||||||
|
|
@ -127,7 +127,7 @@ class AdvancedLogger:
|
||||||
content_clean = content.strip('[]').strip()
|
content_clean = content.strip('[]').strip()
|
||||||
# This is getting complex, let's just try line-by-line fallback
|
# This is getting complex, let's just try line-by-line fallback
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# If not a list or failed list parse, try NDJSON line-by-line
|
# If not a list or failed list parse, try NDJSON line-by-line
|
||||||
if not logs:
|
if not logs:
|
||||||
for line in content.splitlines():
|
for line in content.splitlines():
|
||||||
|
|
@ -139,7 +139,11 @@ class AdvancedLogger:
|
||||||
logs.append(json.loads(line))
|
logs.append(json.loads(line))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
continue # Skip corrupt lines
|
continue # Skip corrupt lines
|
||||||
|
|
||||||
|
# Guard against empty logs list after parsing
|
||||||
|
if not logs:
|
||||||
|
return None
|
||||||
|
|
||||||
# Find the most recent leave event
|
# Find the most recent leave event
|
||||||
leave_events = [log for log in logs if isinstance(log, dict) and log.get("event_type") == "member_leave"]
|
leave_events = [log for log in logs if isinstance(log, dict) and log.get("event_type") == "member_leave"]
|
||||||
if leave_events:
|
if leave_events:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue