数据目录 F2b:剖析的 409、剖析设置的 422 带机读码,界面不再从原话里认是哪一种——服务端用 api/coded.CodedHTTPException:剖析开始不了的五种情况带 code(profile_disabled、datasource_inactive、schema_missing、profile_busy、snapshot_tampered),保存数据源时剖析设置不合规带 code=profile_settings_invalid 和出错的那一项 field(max_queries……),给人看的那句不变;CodedHTTPException 多一个可选的 field;前端 profileBlockOf、profileFieldOfRejection 只认 code 和 field,没带机读码的旧响应按通用提示处理;补后端测试和检查脚本的断言(伪造的 409、422 都带 code)

This commit is contained in:
ThearchyHelios
2026-10-04 03:03:23 +08:00
parent bc615670ef
commit 5b3839f787
11 changed files with 172 additions and 60 deletions
+10 -5
View File
@@ -23,6 +23,7 @@ from fastapi import APIRouter, Depends, Header, HTTPException
from pydantic import BaseModel, Field
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.coded import PROFILE_BUSY, SCHEMA_MISSING, SNAPSHOT_TAMPERED, CodedHTTPException
from app.api.runs import actor_of
from app.data import catalog, catalog_impact, catalog_profile, introspect, table_versions
from app.data.engine import SnapshotTampered
@@ -275,21 +276,25 @@ async def profile_catalog_tables(source_id: str, payload: CatalogProfileIn,
默认关闭:数据源设置里没开启数据剖析、数据源停用,409;同一个数据源已经在剖析,409。
"""
row, source = await _resolved(session, source_id)
# 409 都带机读码(api/coded.py),界面按它分五种情况给下一步,不从原话里认
try:
catalog_profile.ensure_enabled(row)
except catalog_profile.ProfileDisabled as e:
raise HTTPException(409, str(e)) from e
raise CodedHTTPException(409, str(e), e.code) from e
cache = source.schema_cache or {}
if not cache.get("tables"):
raise HTTPException(409, f"{introspect.why_empty(cache)},无法进行数据剖析。请先在数据源卡片上点「探查结构」")
raise CodedHTTPException(409, f"{introspect.why_empty(cache)},无法进行数据剖析。请先在数据源卡片上点「探查结构」",
SCHEMA_MISSING)
actor = actor_of(x_actor)
try:
report = await catalog_profile.profile_catalog(session, row, source, tables=payload.tables or None,
actor=actor)
except (catalog_profile.ProfileDisabled, catalog_profile.ProfileBusy) as e:
raise HTTPException(409, str(e)) from e
except catalog_profile.ProfileDisabled as e:
raise CodedHTTPException(409, str(e), e.code) from e
except catalog_profile.ProfileBusy as e:
raise CodedHTTPException(409, str(e), PROFILE_BUSY) from e
except SnapshotTampered as e:
raise HTTPException(409, str(e)) from e
raise CodedHTTPException(409, str(e), SNAPSHOT_TAMPERED) from e
rows = [_profile_table_out(t) for t in report.tables]
note = None
if not rows and report.empty_tables:
+17 -6
View File
@@ -11,17 +11,28 @@ from fastapi.responses import JSONResponse
#: 限定的数据源一个都不在了(删了或停用了),见 copilot._sources
DATASOURCE_SCOPE_EMPTY = "datasource_scope_empty"
#: 保存数据源时剖析设置不合规(422):field 是出错的那一项(max_queries……),说不清是哪一项时没有 field
PROFILE_SETTINGS_INVALID = "profile_settings_invalid"
#: 剖析开始不了(409)的五种情况,见 api/catalog.profile_catalog_tables
PROFILE_DISABLED = "profile_disabled"
DATASOURCE_INACTIVE = "datasource_inactive"
SCHEMA_MISSING = "schema_missing"
PROFILE_BUSY = "profile_busy"
SNAPSHOT_TAMPERED = "snapshot_tampered"
class CodedHTTPException(HTTPException):
def __init__(self, status_code: int, detail: str, code: str) -> None:
"""field:出错的是哪一项(表单据此把报错落到那一格)。可选,没有就不出现在返回里。"""
def __init__(self, status_code: int, detail: str, code: str, *, field: str | None = None) -> None:
super().__init__(status_code, detail)
self.code = code
self.field = field
async def coded_handler(_: Request, exc: Exception) -> JSONResponse:
assert isinstance(exc, CodedHTTPException)
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail, "code": exc.code},
headers=exc.headers,
)
content: dict[str, str] = {"detail": exc.detail, "code": exc.code}
if exc.field:
content["field"] = exc.field
return JSONResponse(status_code=exc.status_code, content=content, headers=exc.headers)
+7 -4
View File
@@ -17,13 +17,13 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.api import health
from app.api.coded import CodedHTTPException
from app.api.coded import PROFILE_SETTINGS_INVALID, CodedHTTPException
from app.api.runs import actor_of
from app.core.errors import AUTH, NETWORK, TIMEOUT, classify, explain, raw
from app.core.config import settings
from app.core.crypto import encrypt, mask
from app.data import catalog as data_catalog
from app.data.catalog_profile import profile_settings_problem
from app.data.catalog_profile import profile_settings_rejection
from app.data import introspect as introspect_mod
from app.data import table_versions
from app.data.engine import (
@@ -337,10 +337,13 @@ _MASK_FIELD = {
def _refuse_bad_options(options: dict[str, Any] | None) -> None:
problem = (query_timeout_problem(options) or mask_columns_problem(options)
or profile_settings_problem(options))
problem = query_timeout_problem(options) or mask_columns_problem(options)
if problem:
raise HTTPException(422, problem)
# 剖析设置:带机读码和出错的那一项(field),表单按它把报错落到那一格,不再从原话里认
if rejected := profile_settings_rejection(options):
message, field = rejected
raise CodedHTTPException(422, message, PROFILE_SETTINGS_INVALID, field=field)
@router.get("/kinds")