From d14fc97da7f19c2c3bc6d321278295599805c410 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Fri, 16 Oct 2026 10:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=85=A5=E5=8F=A3=E3=80=81?= =?UTF-8?q?=E6=8C=89=E9=9C=80=E8=A7=86=E5=9B=BE=E7=9A=84=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E6=B2=A1=E9=80=9A=E8=BF=87=E6=A0=A1=E9=AA=8C=E4=B9=9F=E5=B8=A6?= =?UTF-8?q?=20code=EF=BC=88form=5Finvalid=EF=BC=89=EF=BC=8C=E5=92=8C?= =?UTF-8?q?=E5=BE=85=E5=8A=9E=E5=9B=9E=E5=A4=8D=E3=80=81=E5=85=A5=E7=AB=99?= =?UTF-8?q?=E9=92=A9=E5=AD=90=E5=90=8C=E4=B8=80=E4=B8=AA=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit api/apps.py 里这两处直接回 JSONResponse(detail + errors),上一次补错误码时只查了 HTTPException,漏了它们:外部系统发起 实例时表单不对,仍然只能按中文句子认。detail 和 errors 不变。 tests/test_integration_errors.py 的静态检查跟着扩:这几个模块里直接回的 JSONResponse 错误,内容里也要有 code(改之前 这条检查报出了这两处)。 --- backend/app/api/apps.py | 6 +++--- backend/app/api/coded.py | 2 ++ backend/tests/test_integration_errors.py | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py index 6d0b281f..8d97e99d 100644 --- a/backend/app/api/apps.py +++ b/backend/app/api/apps.py @@ -21,7 +21,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from app.api.coded import ( APP_BINDINGS_INVALID, APP_DRAFT_INVALID, APP_GRANTS_INVALID, APP_HOOK_NOT_DECLARED, APP_HOOK_NOT_FOUND, - APP_INSTANCE_NOT_FOUND, APP_NOT_FOUND, APP_PARAMS_INVALID, APP_VERSION_NOT_FOUND, RUN_LAUNCH_REFUSED, + APP_INSTANCE_NOT_FOUND, APP_NOT_FOUND, APP_PARAMS_INVALID, APP_VERSION_NOT_FOUND, FORM_INVALID, RUN_LAUNCH_REFUSED, CodedHTTPException, ) from app.api.deps import Principal, actor_of, current_principal @@ -841,7 +841,7 @@ async def start(app_id: str, entry_id: str, payload: StartIn, session: AsyncSess try: run = await start_entry(app.id, entry_id, payload.values, principal) except FormRejected as e: - return JSONResponse(status_code=422, content={"detail": "表单没有通过校验", "errors": e.errors}) + return JSONResponse(status_code=422, content={"detail": "表单没有通过校验", "code": FORM_INVALID, "errors": e.errors}) except (EntryRefused, LaunchRefused) as e: raise _view_refused(e) from e return {"run_id": run.id, "title": instance_title(run)} @@ -912,7 +912,7 @@ async def refresh_view(app_id: str, view_id: str, payload: RefreshIn, session: A run, cached = await run_view(session, app=app, version=version, view_id=view_id, params=payload.params, principal=principal, actor=actor_of(x_actor)) except FormRejected as e: - return JSONResponse(status_code=422, content={"detail": "参数没有通过校验", "errors": e.errors}) + return JSONResponse(status_code=422, content={"detail": "参数没有通过校验", "code": FORM_INVALID, "errors": e.errors}) except (ViewRefused, LaunchRefused) as e: raise _view_refused(e) from e return {"run_id": run.id, "cached": cached} diff --git a/backend/app/api/coded.py b/backend/app/api/coded.py index 27e7ea66..4227b648 100644 --- a/backend/app/api/coded.py +++ b/backend/app/api/coded.py @@ -54,6 +54,8 @@ APP_DRAFT_INVALID = "app_draft_invalid" APP_GRANTS_INVALID = "app_grants_invalid" #: 页面参数不是 JSON 对象(422) APP_PARAMS_INVALID = "app_params_invalid" +#: 入口表单、按需视图的参数没通过校验(422,另带 errors:[{path, code, message}])。同待办回复、入站钩子的表单错误 +FORM_INVALID = "form_invalid" #: 发起实例、刷新视图、补算时引擎不发起运行,又没有更细的码(engine/launch.LaunchRefused 没带 code 的那几种) RUN_LAUNCH_REFUSED = "run_launch_refused" # 派生应用草稿时工作流、版本不存在(404)的码在 apps/derive.py(WORKFLOW_GONE、VERSION_GONE);入口、视图、补算的码 diff --git a/backend/tests/test_integration_errors.py b/backend/tests/test_integration_errors.py index cd8c0262..f3e39b6f 100644 --- a/backend/tests/test_integration_errors.py +++ b/backend/tests/test_integration_errors.py @@ -46,13 +46,23 @@ async def new_app() -> str: return row.id -def test_no_bare_http_exception_left_in_the_integration_modules(): - """这几个模块里不许再直接抛 HTTPException(没有 code):一律 CodedHTTPException。""" +def _called(node: ast.AST, name: str) -> bool: + return isinstance(node, ast.Call) and (getattr(node.func, "id", None) or getattr(node.func, "attr", None)) == name + + +def test_no_error_without_a_code_left_in_the_integration_modules(): + """这几个模块里不许再直接抛 HTTPException(没有 code),一律 CodedHTTPException;直接回 JSONResponse 的错误(表单 + 没通过校验那几处)内容里也要有 code。""" for rel in ("api/apps.py", "api/tasks.py", "api/artifacts.py", "api/deps.py", "auth/__init__.py"): tree = ast.parse((APP_DIR / rel).read_text()) - bare = [node.lineno for node in ast.walk(tree) if isinstance(node, ast.Call) - and (getattr(node.func, "id", None) or getattr(node.func, "attr", None)) == "HTTPException"] + bare = [node.lineno for node in ast.walk(tree) if _called(node, "HTTPException")] assert not bare, f"{rel} 第 {bare} 行抛的 HTTPException 没有 code" + for node in ast.walk(tree): + if not _called(node, "JSONResponse"): + continue + content = next((k.value for k in node.keywords if k.arg == "content"), node.args[0] if node.args else None) + keys = {k.value for k in getattr(content, "keys", []) if isinstance(k, ast.Constant)} + assert "code" in keys, f"{rel} 第 {node.lineno} 行回的 JSONResponse 没有 code" def test_codes_agree_with_the_ones_already_in_use():