Additional exceptions added

This commit is contained in:
Profitroll 2022-08-11 11:27:17 +02:00
parent 673ecaaab9
commit f4dca4164c
1 changed files with 24 additions and 5 deletions

View File

@ -1,24 +1,37 @@
try: try:
import ujson as json import ujson as json
from ujson import JSONDecodeError as JSONDecodeError
except ModuleNotFoundError: except ModuleNotFoundError:
import json import json
from json import JSONDecodeError as JSONDecodeError
import os import os
import sys
import traceback
from signal import SIGKILL from signal import SIGKILL
import psutil from modules.logging import logWrite
def jsonLoad(filename): def jsonLoad(filename):
"""Loads arg1 as json and returns its contents""" """Loads arg1 as json and returns its contents"""
with open(filename, "r", encoding='utf8') as file: with open(filename, "r", encoding='utf8') as file:
output = json.loads(file.read()) try:
output = json.loads(file.read())
except JSONDecodeError:
logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{traceback.print_exc()}")
except FileNotFoundError:
logWrite(f"Could not load json file {filename}: file does not seem to exist!\n{traceback.print_exc()}")
file.close() file.close()
return output return output
def jsonSave(contents, filename): def jsonSave(contents, filename):
"""Dumps dict/list arg1 to file arg2""" """Dumps dict/list arg1 to file arg2"""
with open(filename, "w", encoding='utf8') as file: try:
file.write(json.dumps(contents, ensure_ascii=False, indent=4)) with open(filename, "w", encoding='utf8') as file:
file.close() file.write(json.dumps(contents, ensure_ascii=False, indent=4))
file.close()
except Exception as exp:
logWrite(f"Could not save json file {filename}: {exp}\n{traceback.print_exc()}")
return return
@ -87,6 +100,12 @@ def locale(key: str, *args: list, locale=configGet("locale")):
except KeyError: except KeyError:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"' return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
try:
import psutil
except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
sys.exit()
def killProc(pid): def killProc(pid):
if os.name == "posix": if os.name == "posix":
os.kill(pid, SIGKILL) os.kill(pid, SIGKILL)