From c2b681a03ae017ca75af05e08c731a990fc4631f Mon Sep 17 00:00:00 2001 From: ngfchl Date: Fri, 10 Apr 2026 18:54:50 +0800 Subject: [PATCH] =?UTF-8?q?fixed.=20=E4=BF=AE=E5=A4=8D=E6=8E=A5=E5=8F=A3ra?= =?UTF-8?q?ise=E4=BB=8D=E7=84=B6=E8=BF=94=E5=9B=9E=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/urls.py | 53 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/server/urls.py b/server/urls.py index cb74342..2efbedb 100644 --- a/server/urls.py +++ b/server/urls.py @@ -86,11 +86,54 @@ def auto_load_routers(package_name_prefix=None): class UnifiedNinjaAPI(NinjaExtraAPI): def create_response(self, request, data, *, status=None, headers=None, temporal_response=None): - if not isinstance(data, CommonResponse): - if isinstance(data, tuple) and len(data) == 2: - data, status = data - data = CommonResponse.success(data=data) - return super().create_response(request, data, status=status, temporal_response=temporal_response) + # 1. 如果已经是 CommonResponse,直接放行(避免递归) + if isinstance(data, CommonResponse): + return super().create_response(request, data, status=status, temporal_response=temporal_response) + + # 2. 处理 (data, status) 元组 + if isinstance(data, tuple) and len(data) == 2: + data, status = data + + # 3. 确保 status 有值,默认为 200 + if status is None: + status = 200 + + # ================= 核心修改开始 ================= + # 4. 嗅探数据内容:判断是否是框架抛出的异常数据 + # Django Ninja / DRF 的异常通常格式为 {"detail": "错误信息"} + # 或者 {"errors": [...]} + is_framework_error = False + error_message = "" + + if isinstance(data, dict): + # 情况 A: 捕获到 {"detail": "权限不足"} + if "detail" in data: + is_framework_error = True + error_message = data.get("detail") + # 情况 B: 捕获到其他标准错误格式 (可选) + elif "message" in data and status >= 400: + is_framework_error = True + error_message = data.get("message") + + # 5. 根据嗅探结果决定返回 success 还是 error + if is_framework_error: + # 强制返回 error 结构,succeed=False + # 注意:这里通常使用 403 或 500 作为 code,或者保留原始 status + return super().create_response( + request, + CommonResponse.error(code=status, data=data, msg=error_message), + status=status, + temporal_response=temporal_response + ) + + # 如果不是错误,继续正常的成功逻辑 + if 200 <= status < 300: + wrapped_data = CommonResponse.success(data=data) + else: + # 其他非 200 状态码视为业务错误 + wrapped_data = CommonResponse.error(code=status, data=data) + + return super().create_response(request, wrapped_data, status=status, temporal_response=temporal_response) api_v1 = UnifiedNinjaAPI(version='1.0.0', auth=JWTAuth())