数据目录 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
+11 -2
View File
@@ -37,6 +37,8 @@ export class ApiError extends Error {
* detail 是给人看的话,随时可能改写;要按错误种类分支的认这个
*/
code?: string
/** 带机读码的错误里出错的是哪一项(app/api/coded.py 的 field,比如剖析设置的 max_queries);没有就是 undefined */
field?: string
/**
* 后端要用户先拍板时随错误一起给的 {kind, details}(比如上传表格的 422:数字列混入非数字、
* 交叉表)。原样保留,按接口各自校验后再用(上传表格见 uploadDecision)
@@ -46,7 +48,7 @@ export class ApiError extends Error {
constructor(
public status: number,
message: string,
opts?: { kind?: ApiErrorKind; detail?: unknown; raw?: string; timeoutMs?: number; code?: string; decision?: unknown },
opts?: { kind?: ApiErrorKind; detail?: unknown; raw?: string; timeoutMs?: number; code?: string; field?: string; decision?: unknown },
) {
super(message)
this.name = 'ApiError'
@@ -55,6 +57,7 @@ export class ApiError extends Error {
this.raw = opts?.raw
this.timeoutMs = opts?.timeoutMs
this.code = opts?.code
this.field = opts?.field
this.decision = opts?.decision
}
}
@@ -180,6 +183,12 @@ const codeOf = (body: unknown): string | undefined => {
return typeof code === 'string' && code ? code : undefined
}
/** 后端 {detail, code, field} 里出错的那一项;没有就是 undefined */
const fieldOf = (body: unknown): string | undefined => {
const field = body && typeof body === 'object' ? (body as { field?: unknown }).field : undefined
return typeof field === 'string' && field ? field : undefined
}
/** 后端 {detail, decision} 里要用户拍板的内容:是个对象就原样带上,没有就是 undefined */
const decisionOf = (body: unknown): unknown => {
const decision = body && typeof body === 'object' ? (body as { decision?: unknown }).decision : undefined
@@ -239,7 +248,7 @@ function failure(status: number, statusText: string, text: string, path?: string
const detail = body?.detail ?? body
return new ApiError(status, describeDetail(detail, path), {
detail, raw: typeof body?.raw === 'string' ? body.raw : `${status} ${text.slice(0, 2000)}`,
code: codeOf(body), decision: decisionOf(body),
code: codeOf(body), field: fieldOf(body), decision: decisionOf(body),
})
}
const message = status >= 500
+6 -6
View File
@@ -1039,13 +1039,13 @@ function toBody(form: SourceForm, isNew: boolean, wasOracle = true): any {
}
/**
* 422 的中文 detail 说的是哪一项:查询时限(后端 query_timeout_problem);数据剖析的某一项(profile_settings_problem,
* 记为 profile.<键>,它的「单条查询时限」也带「查询时限」四个字,要先认)。认不出的交回 toast
* 422 说的是哪一项:数据剖析的某一项按机读码和 field 认(profile_settings_rejection,记为 profile.<键>);
* 查询时限(后端 query_timeout_problem)还没有机读码,按 detail 认。认不出的交回 toast
*/
function fieldOfRejection(message: string): string | null {
const profile = profileFieldOfRejection(message)
function fieldOfRejection(e: ApiError): string | null {
const profile = profileFieldOfRejection(e)
if (profile) return `profile.${profile}`
if (/查询时限/.test(message)) return 'query_timeout_s'
if (/查询时限/.test(e.message)) return 'query_timeout_s'
return null
}
@@ -1151,7 +1151,7 @@ function SourceEditor({ source, kinds, onClose, onSaved }: {
onSaved(row, testFresh ? test.result : undefined, connectionOf(body) !== before)
} catch (e) {
const rejected = e instanceof ApiError && e.status === 422 ? e.message : ''
const key = rejected ? fieldOfRejection(rejected) : null
const key = e instanceof ApiError && rejected ? fieldOfRejection(e) : null
if (key?.startsWith('profile.')) {
setFieldError({ key, message: rejected })
focusProfileField('ds-profile', key.slice('profile.'.length) as ProfileErrorKey)
+1 -1
View File
@@ -168,7 +168,7 @@ export function CatalogPage() {
next = { phase: 'done', report, ms: Date.now() - startedAt }
} catch (e) {
next = e instanceof ApiError && e.status === 409
? { phase: 'blocked', kind: profileBlockOf(e.message), message: e.message, tables }
? { phase: 'blocked', kind: profileBlockOf(e), message: e.message, tables }
: { phase: 'failed', error: e, tables }
}
const here = alive.current && sourceRef.current === src
@@ -128,7 +128,7 @@ export function ProfileSettingsDialog({ row, onClose, onSaved }: {
onSaved(next)
toast.ok(form.enabled === initial.enabled ? PT.saved(row.name) : form.enabled ? PT.savedOn(row.name) : PT.savedOff(row.name))
} catch (e) {
const key = e instanceof ApiError && e.status === 422 ? profileFieldOfRejection(e.message) : null
const key = e instanceof ApiError && e.status === 422 ? profileFieldOfRejection(e) : null
if (key) {
setServerError({ key, message: e instanceof ApiError ? e.message : '' })
focusProfileField(idPrefix, key)
+25 -19
View File
@@ -1,5 +1,5 @@
import type { CatalogProfileNumberKey, CatalogProfileSettings } from '../../types'
import { PROFILE_FIELD_LABEL, PROFILE_TEXT } from '../../lib/terms'
import { PROFILE_TEXT } from '../../lib/terms'
// ===========================================================================
// 数据剖析的设置:数据源 options.catalog_profile 和表单之间的来回转换、校验、保存被拒时认出是哪一项。
@@ -120,18 +120,20 @@ export function profileSettingsOfForm(form: ProfileForm): CatalogProfileSettings
export const sameProfileForm = (a: ProfileForm, b: ProfileForm): boolean =>
a.enabled === b.enabled && PROFILE_NUMBER_KEYS.every((k) => a.values[k] === b.values[k])
/** 剖析设置不合规的 422 带的机读码(服务端 api/coded.PROFILE_SETTINGS_INVALID) */
export const PROFILE_SETTINGS_INVALID = 'profile_settings_invalid'
/**
* 服务端保存时拒收的 422(一句中文)说的是剖析设置的哪一项:
* 「数据剖析的「查询次数上限」需要填写 1 到 500 之间的整数;当前为「0」」→ max_queries。
* 说的是剖析设置、但认不出哪一项(格式不对、有认不出的项)返回 'section';说的不是剖析设置返回 null
* 服务端保存时拒收的 422 说的是剖析设置的哪一项:按机读码和 field 认(服务端 profile_settings_rejection),不从原话里认——
* 文案一改,按原话认的就悄悄失效。说的是剖析设置、但没有 field(格式不对、有认不出的项)返回 'section';
* 不是剖析设置的、没带机读码的(老服务端)返回 null,交给通用的报错提示
*/
export function profileFieldOfRejection(message: string): 'enabled' | CatalogProfileNumberKey | 'section' | null {
if (!message.includes('数据剖析')) return null
const named = message.match(/「([^」]+)」/)?.[1] ?? ''
for (const k of ['enabled', ...PROFILE_NUMBER_KEYS] as const) {
if (named && named.startsWith(PROFILE_FIELD_LABEL[k])) return k
}
return 'section'
export function profileFieldOfRejection(e: { code?: string; field?: string }): 'enabled' | CatalogProfileNumberKey | 'section' | null {
if (e.code !== PROFILE_SETTINGS_INVALID) return null
const field = e.field ?? ''
return field === 'enabled' || (PROFILE_NUMBER_KEYS as readonly string[]).includes(field)
? field as 'enabled' | CatalogProfileNumberKey
: 'section'
}
// ---------------------------------------------------------------------------
@@ -156,16 +158,20 @@ export function secondsText(ms: number): string {
return s < 60 ? `${s} 秒` : `${Math.floor(s / 60)} 分 ${String(s % 60).padStart(2, '0')} 秒`
}
/** 剖析接口回 409 的几种情况(按服务端的原话认:catalog_profile.ensure_enabled、api/catalog.py、engine.SnapshotTampered) */
/** 剖析接口回 409 的几种情况:按机读码认(服务端 api/coded.py,api/catalog.profile_catalog_tables 带上) */
export type ProfileBlock = 'disabled' | 'inactive' | 'noSchema' | 'busy' | 'tampered' | 'other'
export function profileBlockOf(message: string): ProfileBlock {
if (message.includes('未开启数据剖析')) return 'disabled'
if (message.includes('已停用')) return 'inactive'
if (message.includes('正在进行数据剖析')) return 'busy'
if (message.includes('探查结构')) return 'noSchema'
if (/已拒绝(?:这次)?查询/.test(message)) return 'tampered'
return 'other'
const PROFILE_BLOCK_CODES: Record<string, ProfileBlock> = {
profile_disabled: 'disabled',
datasource_inactive: 'inactive',
schema_missing: 'noSchema',
profile_busy: 'busy',
snapshot_tampered: 'tampered',
}
/** 没带机读码(老服务端)或认不出的按「无法开始剖析」说,给重试;不从原话里猜 */
export function profileBlockOf(e: { code?: string }): ProfileBlock {
return (e.code && PROFILE_BLOCK_CODES[e.code]) || 'other'
}
/** 设置里有没有剖析设置这一项(options 的其余键原样带上) */