应用入口、按需视图的表单没通过校验也带 code(form_invalid),和待办回复、入站钩子同一个码

api/apps.py 里这两处直接回 JSONResponse(detail + errors),上一次补错误码时只查了 HTTPException,漏了它们:外部系统发起
实例时表单不对,仍然只能按中文句子认。detail 和 errors 不变。

tests/test_integration_errors.py 的静态检查跟着扩:这几个模块里直接回的 JSONResponse 错误,内容里也要有 code(改之前
这条检查报出了这两处)。
This commit is contained in:
ThearchyHelios
2026-10-16 10:33:41 +08:00
parent 2fb16c2719
commit d14fc97da7
3 changed files with 19 additions and 7 deletions
+3 -3
View File
@@ -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}
+2
View File
@@ -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);入口、视图、补算的码
+14 -4
View File
@@ -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():