summaryrefslogtreecommitdiff
path: root/utils/utils_error
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2020-10-07 13:03:27 +0800
committerKyomotoi <[email protected]>2020-10-07 13:03:27 +0800
commitcec804951b97bcab81551bb8c7a1a1e1c473aaa7 (patch)
tree03b7204af6ebc869e887494ab2609a6e9b7fd72c /utils/utils_error
parentab467e8788b7ef8382bab63fb1a91c8b6305c501 (diff)
downloadATRI-cec804951b97bcab81551bb8c7a1a1e1c473aaa7.tar.gz
ATRI-cec804951b97bcab81551bb8c7a1a1e1c473aaa7.tar.bz2
ATRI-cec804951b97bcab81551bb8c7a1a1e1c473aaa7.zip
[Update]
Diffstat (limited to 'utils/utils_error')
-rw-r--r--utils/utils_error/__init__.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/utils/utils_error/__init__.py b/utils/utils_error/__init__.py
new file mode 100644
index 0000000..e037b30
--- /dev/null
+++ b/utils/utils_error/__init__.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import json
+import string
+from pathlib import Path
+from random import sample
+from typing import Optional
+from datetime import datetime
+from traceback import format_exc
+
+from nonebot.rule import keyword
+
+def errorRepo(repo_msg: Optional[str] = None) -> str:
+ """
+ :说明:
+
+ 返回错误堆栈。
+
+ :参数:
+
+ * ``repo_msg: Optional[str] = None``: 此错误发生时指定的错误信息,若不传入则返回 unknown
+
+ :返回:
+
+ 错误信息
+
+ :用法:
+
+ .. code-block:: python
+
+ try:
+ ...
+ except Exception:
+ print(errorRepo(repo_msg="message"))
+
+ """
+ file_error = Path('.') / 'ATRI' / 'data' / 'data_Error' / 'error.json'
+ try:
+ with open(file_error, 'r') as f:
+ data_error = json.load(f)
+ except:
+ data_error = {}
+
+ key_error = ''.join(sample(string.ascii_letters + string.digits, 16))
+ msg_error = f"{datetime.now()}\n"
+ msg_error = f"{format_exc()}"
+ data_error[f"{key_error}"] = f"{msg_error}"
+
+ with open(file_error, 'w') as f:
+ f.write(json.dumps(data_error))
+ f.close()
+
+ if repo_msg:
+ pass
+ else:
+ repo_msg = 'unknown'
+
+ msg0 = f'ERROR! Reason: [{repo_msg}]\n'
+ msg0 += f'trackID: {key_error}\n'
+ msg0 += "请使用[来杯红茶]功能以联系维护者\n"
+ msg0 += "并附上 trackID"
+
+ return msg0 \ No newline at end of file