소스 검색

优化返回时间

SpringSunYY 4 달 전
부모
커밋
438eac2b95
2개의 변경된 파일25개의 추가작업 그리고 1개의 파일을 삭제
  1. 5 1
      ruoyi_common/__init__.py
  2. 20 0
      ruoyi_common/base/serializer.py

+ 5 - 1
ruoyi_common/__init__.py

@@ -6,10 +6,11 @@ import sys
 from types import ModuleType
 from werkzeug.exceptions import HTTPException
 
-from ruoyi_common.base.serializer import JsonProvider,handle_http_exception
+from ruoyi_common.base.serializer import JsonProvider,handle_http_exception,handle_util_exception
 from ruoyi_common.descriptor.listener import ModuleSignalListener
 from ruoyi_common.base.signal import module_initailize
 from ruoyi_common.ruoyi.registry import RuoYiModuleRegistry
+from ruoyi_common.utils.base import UtilException
 
 
 @ModuleSignalListener(sys.modules[__name__],module_initailize)
@@ -31,3 +32,6 @@ def import_hook(module:ModuleType, registry:RuoYiModuleRegistry):
     registry.app.register_error_handler(
         HTTPException, handle_http_exception
     )
+    registry.app.register_error_handler(
+        UtilException, handle_util_exception
+    )

+ 20 - 0
ruoyi_common/base/serializer.py

@@ -12,6 +12,8 @@ from werkzeug.exceptions import HTTPException, default_exceptions
 from werkzeug.http import http_date
 
 from ruoyi_common.base.model import AjaxResponse
+from ruoyi_common.constant import HttpStatus
+from ruoyi_common.utils.base import UtilException
 
 WSGIEnvironment: t.TypeAlias = dict[str, t.Any]
 
@@ -187,3 +189,21 @@ def handle_http_exception(error:HTTPException) -> Response:
     if not isinstance(error, HttpException):
         error = HttpException.from_http_exception(error)
     return error.get_response()
+
+
+def handle_util_exception(error:UtilException) -> Response:
+    """
+    处理业务工具类异常,保持和若依Java版一致的json结构
+    """
+    status = getattr(error, "status", HttpStatus.ERROR)
+    ajax_response = AjaxResponse.from_error(msg=str(error))
+    ajax_response.code = status
+    response = Response(
+        response=ajax_response.model_dump_json(
+            exclude_unset=True,
+            exclude_none=True,
+        ),
+        status=status,
+        mimetype="application/json"
+    )
+    return response