fix: handle analytics file access errors gracefully

This commit is contained in:
Paul Gauthier (aider) 2024-11-26 20:37:29 -08:00
parent f702f67e27
commit 7dffa943fa

View file

@ -105,9 +105,14 @@ class Analytics:
return uuid_str[:6] <= threshold
def get_data_file_path(self):
try:
data_file = Path.home() / ".aider" / "analytics.json"
data_file.parent.mkdir(parents=True, exist_ok=True)
return data_file
except OSError:
# If we can't create/access the directory, just disable analytics
self.disable(permanently=False)
return None
def get_or_create_uuid(self):
self.load_data()
@ -119,6 +124,9 @@ class Analytics:
def load_data(self):
data_file = self.get_data_file_path()
if not data_file:
return
if data_file.exists():
try:
data = json.loads(data_file.read_text())
@ -130,14 +138,20 @@ class Analytics:
def save_data(self):
data_file = self.get_data_file_path()
if not data_file:
return
data = dict(
uuid=self.user_id,
permanently_disable=self.permanently_disable,
asked_opt_in=self.asked_opt_in,
)
# Allow exceptions; crash if we can't record permanently_disabled=True, etc
try:
data_file.write_text(json.dumps(data, indent=4))
except OSError:
# If we can't write the file, just disable analytics
self.disable(permanently=False)
def get_system_info(self):
return {