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():