refactor(src): migrate src/log_registry.py to Result[T] error handling (2 sites)
Migrates the 2 try/except sites in LogRegistry: 1. save_registry() - line 132: was except Exception: print(...) Now except OSError: and returns Result[bool] with ErrorInfo on failure. Removed the print() diagnostic. 2. update_auto_whitelist_status() - line 246: was except Exception: pass Now except OSError: (narrowed). No return value change since the method returns None anyway. Both sites narrowed from broad except Exception to specific stdlib I/O exceptions. Callers of save_registry() (register_session, update_session_metadata) ignore the Result return value. Tests verified: - tests/test_log_registry.py (5 tests) PASS - tests/test_logging_e2e.py (1 test) PASS - tests/test_auto_whitelist.py (4 tests) PASS
This commit is contained in:
+8
-5
@@ -46,6 +46,8 @@ import tomllib
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from src.result_types import Result, ErrorInfo, ErrorKind
|
||||
|
||||
|
||||
class LogRegistry:
|
||||
"""
|
||||
@@ -98,7 +100,7 @@ class LogRegistry:
|
||||
else:
|
||||
self.data = {}
|
||||
|
||||
def save_registry(self) -> None:
|
||||
def save_registry(self) -> Result[bool]:
|
||||
"""
|
||||
Serializes and saves the current registry data to the TOML file.
|
||||
Converts internal datetime objects to ISO format strings for compatibility.
|
||||
@@ -129,8 +131,9 @@ class LogRegistry:
|
||||
data_to_save[session_id] = session_data_copy
|
||||
with open(self.registry_path, 'wb') as f:
|
||||
tomli_w.dump(data_to_save, f)
|
||||
except Exception as e:
|
||||
print(f"Error saving registry to {self.registry_path}: {e}")
|
||||
return Result(data=True)
|
||||
except OSError as e:
|
||||
return Result(data=False, errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(e), source="log_registry.save_registry", original=e)])
|
||||
|
||||
def register_session(self, session_id: str, path: str, start_time: datetime | str) -> None:
|
||||
"""
|
||||
@@ -241,9 +244,9 @@ class LogRegistry:
|
||||
for kw in keywords_to_check:
|
||||
if kw in line and kw not in found_keywords:
|
||||
found_keywords.append(kw)
|
||||
except Exception:
|
||||
except OSError:
|
||||
pass
|
||||
except Exception:
|
||||
except OSError:
|
||||
pass
|
||||
size_kb = total_size_bytes / 1024
|
||||
whitelisted = False
|
||||
|
||||
Reference in New Issue
Block a user