File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -118,12 +118,21 @@ def get(self, key: str) -> str | None:
118118
119119 s = time .perf_counter ()
120120
121- with store .KeyValueStore (self ._file , flag = "r" ) as db :
121+ try :
122+ db_ctx = store .KeyValueStore (self ._file , flag = "r" )
123+ except sqlite3 .DatabaseError as ex :
124+ LOG .warning (f"Failed to open cache database: { ex } " )
125+ return None
126+
127+ with db_ctx as db :
122128 try :
123129 raw_payload : bytes | None = db .get (key ) # data retrieved from db[key]
124130 except Exception as ex :
125131 if self ._table_not_found (ex ):
126132 return None
133+ if isinstance (ex , sqlite3 .DatabaseError ):
134+ LOG .warning (f"Cache read error for { key } : { ex } " )
135+ return None
127136 raise ex
128137
129138 if raw_payload is None :
Original file line number Diff line number Diff line change @@ -372,6 +372,20 @@ def test_keys_read_mode_corrupted_database(tmpdir):
372372 assert all (isinstance (key , str ) for key in keys )
373373
374374
375+ def test_get_returns_none_on_corrupt_database_file (tmpdir ):
376+ """Test that get() returns None instead of raising when the database is corrupt."""
377+ cache_file = os .path .join (tmpdir , "cache_corrupt" )
378+ cache = PersistentCache (cache_file )
379+
380+ cache .set ("key1" , "value1" )
381+ assert cache .get ("key1" ) == "value1"
382+
383+ with open (cache_file , "wb" ) as f :
384+ f .write (b"this is not a valid sqlite database file" )
385+
386+ assert cache .get ("key1" ) is None
387+
388+
375389def test_multithread_shared_cache_comprehensive (tmpdir ):
376390 """Test shared cache instance across multiple threads using get->set pattern.
377391
You can’t perform that action at this time.
0 commit comments