@@ -112,33 +112,15 @@ async def get_message_content(
112112 except (aleph_json .DecodeError , json .decoder .JSONDecodeError ) as e :
113113 error_msg = f"Can't decode JSON: { e } "
114114 LOGGER .warning (error_msg )
115- # If content was from local cache and is corrupted, delete it and retry
116115 if source == ContentSource .DB and item_type in (
117116 ItemType .ipfs ,
118117 ItemType .storage ,
119118 ):
120- LOGGER .warning (
121- f"Corrupted cached content for { item_hash } , deleting and retrying from network"
122- )
123- await self .storage_engine .delete (filename = item_hash )
124- hash_content = await self .get_hash_content (
125- item_hash ,
126- engine = ItemType (item_type ),
127- use_network = True ,
128- use_ipfs = True ,
119+ content , hash_content = await self ._recover_cached_content (
120+ item_hash , ItemType (item_type )
129121 )
130122 item_content = hash_content .value
131123 source = hash_content .source
132- # Try parsing again
133- try :
134- content = aleph_json .loads (item_content )
135- except (
136- aleph_json .DecodeError ,
137- json .decoder .JSONDecodeError ,
138- ) as retry_error :
139- raise InvalidContent (
140- f"Content still invalid after retry: { retry_error } "
141- ) from retry_error
142124 else :
143125 raise InvalidContent (error_msg )
144126
@@ -149,6 +131,31 @@ async def get_message_content(
149131 raw_value = item_content ,
150132 )
151133
134+ async def _recover_cached_content (
135+ self , item_hash : str , engine : ItemType
136+ ) -> tuple [Any , RawContent ]:
137+ """Delete a corrupt cache entry, refetch from network, and parse JSON.
138+
139+ Called when cached content fails either SHA-256 verification or JSON
140+ decoding. Raises InvalidContent if the fresh copy also fails to parse.
141+ """
142+ LOGGER .warning (
143+ "Corrupted cached content for %s, deleting and retrying from network" ,
144+ item_hash ,
145+ )
146+ await self .storage_engine .delete (filename = item_hash )
147+ hash_content = await self .get_hash_content (
148+ item_hash ,
149+ engine = engine ,
150+ use_network = True ,
151+ use_ipfs = True ,
152+ )
153+ try :
154+ content = aleph_json .loads (hash_content .value )
155+ except (aleph_json .DecodeError , json .decoder .JSONDecodeError ) as e :
156+ raise InvalidContent (f"Content still invalid after retry: { e } " ) from e
157+ return content , hash_content
158+
152159 async def _fetch_content_from_network (
153160 self , content_hash : str , engine : ItemType , timeout : int
154161 ) -> Optional [bytes ]:
0 commit comments