@@ -85,14 +85,39 @@ async def get_message_content(
8585
8686 try :
8787 content = aleph_json .loads (item_content )
88- except aleph_json .DecodeError as e :
89- error_msg = f"Can't decode JSON: { e } "
90- LOGGER .warning (error_msg )
91- raise InvalidContent (error_msg )
92- except json .decoder .JSONDecodeError as e :
88+ except (aleph_json .DecodeError , json .decoder .JSONDecodeError ) as e :
9389 error_msg = f"Can't decode JSON: { e } "
9490 LOGGER .warning (error_msg )
95- raise InvalidContent (error_msg )
91+ # If content was from local cache and is corrupted, delete it and retry
92+ if source == ContentSource .DB and item_type in (
93+ ItemType .ipfs ,
94+ ItemType .storage ,
95+ ):
96+ LOGGER .warning (
97+ f"Corrupted cached content for { item_hash } , deleting and retrying from network"
98+ )
99+ await self .storage_engine .delete (filename = item_hash )
100+ # Retry fetching from network/IPFS
101+ hash_content = await self .get_hash_content (
102+ item_hash ,
103+ engine = ItemType (item_type ),
104+ use_network = True ,
105+ use_ipfs = True ,
106+ )
107+ item_content = hash_content .value
108+ source = hash_content .source
109+ # Try parsing again
110+ try :
111+ content = aleph_json .loads (item_content )
112+ except (
113+ aleph_json .DecodeError ,
114+ json .decoder .JSONDecodeError ,
115+ ) as retry_error :
116+ raise InvalidContent (
117+ f"Content still invalid after retry: { retry_error } "
118+ ) from retry_error
119+ else :
120+ raise InvalidContent (error_msg )
96121
97122 return MessageContent (
98123 hash = item_hash ,
@@ -195,16 +220,8 @@ async def get_hash_content(
195220
196221 # Try to retrieve the data from the DB, then from the network or IPFS.
197222 content = await self .storage_engine .read (filename = content_hash )
198-
199223 if content is not None :
200- # check json and fix if corrupted
201- try :
202- json_content = aleph_json .loads (content )
203- source = ContentSource .DB
204- except orjson .JSONDecodeError as e :
205- LOGGER .warning ("Can't decode JSON, Change source..." )
206- await self .storage_engine .delete (filename = content_hash )
207- content = None
224+ source = ContentSource .DB
208225
209226 if content is None and use_network :
210227 content = await self ._fetch_content_from_network (
0 commit comments