refactor: improve SQLite error handling and logging in tags cache

This commit is contained in:
Paul Gauthier 2024-10-29 15:09:22 -07:00 committed by Paul Gauthier (aider)
parent adca062081
commit 0424e4b00a

View file

@ -169,6 +169,10 @@ class RepoMap:
def tags_cache_error(self, original_error=None): def tags_cache_error(self, original_error=None):
"""Handle SQLite errors by trying to recreate cache, falling back to dict if needed""" """Handle SQLite errors by trying to recreate cache, falling back to dict if needed"""
if self.verbose and original_error:
self.io.tool_warning(f"Tags cache error: {str(original_error)}")
if isinstance(getattr(self, "TAGS_CACHE", None), dict): if isinstance(getattr(self, "TAGS_CACHE", None), dict):
return return
@ -199,8 +203,6 @@ class RepoMap:
f"Unable to use tags cache at {path}, falling back to memory cache" f"Unable to use tags cache at {path}, falling back to memory cache"
) )
if self.verbose: if self.verbose:
if original_error:
self.io.tool_warning(f"Original error: {str(original_error)}")
self.io.tool_warning(f"Cache recreation error: {str(e)}") self.io.tool_warning(f"Cache recreation error: {str(e)}")
self.TAGS_CACHE = dict() self.TAGS_CACHE = dict()
@ -237,8 +239,8 @@ class RepoMap:
if val is not None and val.get("mtime") == file_mtime: if val is not None and val.get("mtime") == file_mtime:
try: try:
return self.TAGS_CACHE[cache_key]["data"] return self.TAGS_CACHE[cache_key]["data"]
except SQLITE_ERRORS: except SQLITE_ERRORS as e:
self.tags_cache_error() self.tags_cache_error(e)
return self.TAGS_CACHE[cache_key]["data"] return self.TAGS_CACHE[cache_key]["data"]
# miss! # miss!
@ -248,8 +250,8 @@ class RepoMap:
try: try:
self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data} self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
self.save_tags_cache() self.save_tags_cache()
except SQLITE_ERRORS: except SQLITE_ERRORS as e:
self.tags_cache_error() self.tags_cache_error(e)
self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data} self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
return data return data
@ -352,8 +354,8 @@ class RepoMap:
try: try:
cache_size = len(self.TAGS_CACHE) cache_size = len(self.TAGS_CACHE)
except SQLITE_ERRORS: except SQLITE_ERRORS as e:
self.tags_cache_error() self.tags_cache_error(e)
cache_size = len(self.TAGS_CACHE) cache_size = len(self.TAGS_CACHE)
if len(fnames) - cache_size > 100: if len(fnames) - cache_size > 100: