niming_igapi/utils/tbProcessor.py

54 lines
1.4 KiB
Python
Raw Normal View History

2024-11-22 02:14:01 +08:00
import json
import traceback
import os
2024-12-18 03:06:40 +08:00
import logging
2024-11-22 02:14:01 +08:00
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
2024-12-18 03:06:40 +08:00
for s in exc_traceback:
logging.error(s) # must display
2024-11-22 02:14:01 +08:00
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)
2024-12-18 03:06:40 +08:00
logging.error(f"Exception id {exc_saved_id} : {exc_type} : {exc_message}")