niming_igapi/utils/tbProcessor.py

51 lines
1.3 KiB
Python
Raw Normal View History

2024-11-22 02:14:01 +08:00
import json
import traceback
import os
FILENAME = "./traceback.json"
def prechecker():
if not os.path.exists(FILENAME):
with open(FILENAME, "w", encoding = "utf-8") as f:
json.dump({"err":{}, "id":0}, f, ensure_ascii=False)
def load():
prechecker()
with open(FILENAME, "r", encoding = "utf-8") as f:
d = json.load(f)
return d
def debug_info_from_exception(exc) -> dict:
exc_type = type(exc).__name__
exc_message = str(exc)
exc_traceback = traceback.format_exception(type(exc), exc, exc.__traceback__)
debug_info = {
"Exception_type": str(exc_type),
"Exception_message": str(exc_message),
"Trackback": str(exc_traceback)
}
# debug
for s in exc_traceback: print(s)
return debug_info
def write(e:Exception):
d:dict = load()
eid = d["id"]
debug_info = debug_info_from_exception(e)
d["err"][str(eid)] = debug_info
d["id"] += 1
with open(FILENAME, "w", encoding = "utf-8") as f:
json.dump(d, f, ensure_ascii=False)
return eid
def easyExceptionHandler(e:Exception):
exc_type = type(e).__name__
exc_message = str(e)
exc_saved_id = write(e)
print(f"[X] Exception id {exc_saved_id} : {exc_type} : {exc_message}")