From 55c956ad4ec7fe0ef0cbfb0e740bfcb1783aa88f Mon Sep 17 00:00:00 2001 From: Dylan Date: Sun, 22 May 2022 15:19:28 +0100 Subject: [PATCH] Error checking for loading cache json file --- twitfix.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/twitfix.py b/twitfix.py index 59e445d..98d8185 100644 --- a/twitfix.py +++ b/twitfix.py @@ -74,9 +74,18 @@ if link_cache_system == "json": default_link_cache = {"test":"test"} json.dump(default_link_cache, outfile, indent=4, sort_keys=True) - f = open('links.json',) - link_cache = json.load(f) - f.close() + try: + f = open('links.json',) + link_cache = json.load(f) + except json.decoder.JSONDecodeError: + print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.") + link_cache = {} + except FileNotFoundError: + print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.") + link_cache = {} + finally: + f.close() + elif link_cache_system == "db": client = pymongo.MongoClient(config['config']['database'], connect=False) table = config['config']['table']