Ver código fonte

add: 自定义异常

yq 5 meses atrás
pai
commit
7112b1bb07

+ 2 - 1
commom/__init__.py

@@ -4,6 +4,7 @@
 @time: 2021/11/9
 @desc: 
 """
+from .user_exceptions import GeneralException
 from .utils import f_get_clazz_in_module, f_clazz_to_json
 
-__all__ = ['f_get_clazz_in_module', 'f_clazz_to_json']
+__all__ = ['f_get_clazz_in_module', 'f_clazz_to_json', 'GeneralException']

+ 16 - 0
commom/user_exceptions.py

@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+"""
+@author: yq
+@time: 2024/11/8
+@desc: 自定义异常
+"""
+from enums import ResultCodesEnum
+
+
+class GeneralException(Exception):
+    def __init__(self, result_codes_enum: ResultCodesEnum, *args, message: str = "", **kwargs):
+        self.message = message
+        self.result_codes_enum = result_codes_enum
+
+    def __str__(self):
+        return f"{self.result_codes_enum.code} {self.result_codes_enum.message} {self.message}"

+ 14 - 0
db_script/mysql/test.sql

@@ -0,0 +1,14 @@
+CREATE TABLE `t1` (
+  `id` bigint(20) NOT NULL AUTO_INCREMENT,
+  `c1` FLOAT NULL,
+  `c2` FLOAT NULL,
+  `c3` FLOAT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='指标测试表';
+
+INSERT INTO test.t1
+(id, c1, c2, c3)
+VALUES(1, 1.0, 2.0, 3.0);
+INSERT INTO test.t1
+(id, c1, c2, c3)
+VALUES(2, 2.0, 3.0, 4.0);

+ 5 - 6
entitys/monitor_metric_config_entity.py

@@ -8,7 +8,9 @@ import json
 import os
 from typing import List, Dict
 
+from commom import GeneralException
 from entitys import MetricConfigEntity
+from enums import ResultCodesEnum
 from metrics import f_get_metric_clazz_dict, MetricBase
 
 
@@ -34,12 +36,10 @@ class MonitorMetricConfigEntity():
             metric_code = metric_config.metric_code
             # 指标函数不存在
             if metric_func_name not in self._metric_clazz_dict.keys():
-                # TODO
-                pass
+                raise GeneralException(ResultCodesEnum.NOT_FOUND, message = f"指标函数【{metric_func_name}】不存在")
             # 指标code不唯一
             if metric_code in metric_dict.keys():
-                # TODO
-                pass
+                raise GeneralException(ResultCodesEnum.ILLEGAL_PARAMS, message = f"指标code【{metric_code}】不唯一")
             metric_clazz = self._metric_clazz_dict[metric_func_name]
             metric_dict[metric_code] = metric_clazz(*metric_config.args, **metric_config.kwargs)
         return metric_dict
@@ -53,8 +53,7 @@ class MonitorMetricConfigEntity():
             with open(config_path, mode="r", encoding="utf-8") as f:
                 j = json.loads(f.read())
         else:
-            # TODO
-            pass
+            raise GeneralException(ResultCodesEnum.NOT_FOUND, message = f"指标监控配置文件【{config_path}】不存在")
         metric_config_list = j.get("metric_config_list", [])
         metric_config_list = [MetricConfigEntity(**i) for i in metric_config_list]
         j["metric_config_list"] = metric_config_list

+ 10 - 0
enums/__init__.py

@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+"""
+@author: yq
+@time: 2024/10/30
+@desc: 枚举值
+"""
+
+from .result_codes_enum import ResultCodesEnum
+
+__all__ = ['ResultCodesEnum']

+ 25 - 0
enums/result_codes_enum.py

@@ -0,0 +1,25 @@
+# -*- coding:utf-8 -*-
+"""
+@author: yq
+@time: 2023/9/18
+@desc: 结果状态枚举值
+"""
+
+from enum import Enum
+
+
+class ResultCodesEnum(Enum):
+
+    SUCCESS = {"code": "200", "message": "success"}
+    SYSTEM_ERROR = {"code": "1001", "message": "system error"}
+    TIME_OUT = {"code": "1002", "message": "time out"}
+    ILLEGAL_PARAMS = {"code": "1003", "message": "illegal params"}
+    NOT_FOUND = {"code": "1004", "message": "not found"}
+
+    @property
+    def message(self):
+        return self.value['message']
+
+    @property
+    def code(self):
+        return self.value['code']