test: add exception trapping and printing info
This commit is contained in:
parent
da3e36c8b5
commit
1eff29f446
|
@ -482,19 +482,39 @@ class TBase:
|
|||
|
||||
# generate new json file
|
||||
def genNewJson(self, jsonFile, modifyFunc=None):
|
||||
with open(jsonFile, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
try:
|
||||
with open(jsonFile, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
tdLog.info(f"the specified json file '{jsonFile}' was not found.")
|
||||
return None
|
||||
except Exception as e:
|
||||
tdLog.info(f"error reading the json file: {e}")
|
||||
return None
|
||||
|
||||
if callable(modifyFunc):
|
||||
modifyFunc(data)
|
||||
|
||||
tempDir = os.path.join(tempfile.gettempdir(), 'json_templates')
|
||||
os.makedirs(tempDir, exist_ok=True)
|
||||
try:
|
||||
os.makedirs(tempDir, exist_ok=True)
|
||||
except PermissionError:
|
||||
tdLog.info(f"no sufficient permissions to create directory at '{tempDir}'.")
|
||||
return None
|
||||
except Exception as e:
|
||||
tdLog.info(f"error creating temporary directory: {e}")
|
||||
return None
|
||||
|
||||
tempPath = os.path.join(tempDir, f"temp_{uuid.uuid4().hex}.json")
|
||||
|
||||
with open(tempPath, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
try:
|
||||
with open(tempPath, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
except Exception as e:
|
||||
tdLog.info(f"error writing to temporary json file: {e}")
|
||||
return None
|
||||
|
||||
tdLog.info(f"create temporary json file successfully, file: {tempPath}")
|
||||
return tempPath
|
||||
|
||||
# delete file
|
||||
|
|
Loading…
Reference in New Issue