From 45e9f1053a4b98696f0c8a4ee289f833bc5cef81 Mon Sep 17 00:00:00 2001 From: ssw Date: Tue, 21 Jul 2026 17:45:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(dacli):=20P5=20sample=209=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20throw=20new=20Error=20=E2=86=92=20DacliError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 命令路径 9 文件 throw new Error(改 DacliError (提升 L2 catch 一致性): - datasource/test.ts (2 throws) — wrapCommand 内 throw - integration/{emailtest,smstest,webhooktest}.ts (8 throws) — input validation - report/{info,list}.ts (2 throws) — input failure - storage/info.ts:38 (1 throw) — platform 不存在错误 - user/{auth-list,batch-state}.ts (2 throws) — 用户管理 跳过: - test/{run,render,generate} (10 处) — 单元测试 intentional throws - form/_*.ts SDK helper (_ai/_browser_verify/_buildformdata 等) — 内部 SDK 边界 - _ssw_retry, apis/_cache — 网络重试/缓存 helper - menu/_helpers.ts — utility 为什么 DacliError 而不是 plain Error: - DacliError.code 字段 (-1 默认) 让 L2 catch 可分类 (wrapCommand 自动 catch) - DacliError.raw 字段保留后端原响应 - DacliError extends Error, 现有 (e as Error).message 写法仍兼容 验证: - TS compile 56 baseline 不变 - 端到端抽样: storage info nonexistent / datasource test --help / integration webhooktest --help 都正常 --- src/commands/datasource/test.ts | 5 +++-- src/commands/integration/emailtest.ts | 5 +++-- src/commands/integration/smstest.ts | 5 +++-- src/commands/integration/webhooktest.ts | 9 +++++---- src/commands/report/info.ts | 3 ++- src/commands/report/list.ts | 3 ++- src/commands/storage/info.ts | 3 ++- src/commands/user/auth-list.ts | 3 ++- src/commands/user/batch-state.ts | 3 ++- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/commands/datasource/test.ts b/src/commands/datasource/test.ts index 19089cd..2b4f0b6 100644 --- a/src/commands/datasource/test.ts +++ b/src/commands/datasource/test.ts @@ -11,6 +11,7 @@ import { log, printJson } from '../../core/log.ts' import { encryptAesEcb, isEncrypted } from '../../core/crypto.ts' import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' +import { DacliError } from '../../core/types.ts' import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘; v2.2+ wrapCommand helper 收口 5c 样板 interface TestOpts { @@ -68,12 +69,12 @@ export function buildDatasourceTestCommand(): Command { oracleExtend: opts.dbType === 'Oracle' ? 1 : 0, } } else { - throw new Error('请传 --id <数据源 id> 或 --dbType + --host + --user + --password') + throw new DacliError('请传 --id <数据源 id> 或 --dbType + --host + --user + --password') } const r = (await loadDataSource.testDbConnection(body)) as { code?: number; msg?: string; data?: unknown } if (r.code !== undefined && r.code !== 200) { const enhanced = enhanceFromBackend(r.msg || '', 'datasource', { entity: 'datasource' }) - throw new Error(formatErrorLine(enhanced, { step: 0, stepName: '后端API' })) + throw new DacliError(formatErrorLine(enhanced, { step: 0, stepName: '后端API' })) } log.info(`✓ 数据源连接成功`) const summary = { id: opts.id, dbType: opts.dbType || body.dbType as string, ok: true } diff --git a/src/commands/integration/emailtest.ts b/src/commands/integration/emailtest.ts index 5296da0..bb6181d 100644 --- a/src/commands/integration/emailtest.ts +++ b/src/commands/integration/emailtest.ts @@ -14,6 +14,7 @@ import { log } from '../../core/log.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 +import { DacliError } from '../../core/types.ts' interface EmailTestOpts { configId: string email?: string @@ -46,7 +47,7 @@ export function buildIntegrationEmailTestCommand(): Command { const configR = await c.get>(`/message/AccountConfig/${opts.configId}`) if (configR.code !== 200 || !configR.data) { const enhanced = enhanceFromBackend(configR.msg || 'id 不存在', 'configId', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) + throw new DacliError(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) } const config = configR.data @@ -63,7 +64,7 @@ export function buildIntegrationEmailTestCommand(): Command { const r = await c.postJson('/message/AccountConfig/testSendMail', body) if (r.code !== 200) { const enhanced = enhanceFromBackend(r.msg || '', 'email', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) + throw new DacliError(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) } log.info(`✓ 邮件测试发送 OK (configId=${opts.configId}, to=${opts.email})`) cmdSummary = { configId: opts.configId, email: opts.email, ok: true } diff --git a/src/commands/integration/smstest.ts b/src/commands/integration/smstest.ts index 3544ca2..cc73ebe 100644 --- a/src/commands/integration/smstest.ts +++ b/src/commands/integration/smstest.ts @@ -14,6 +14,7 @@ import { log } from '../../core/log.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 +import { DacliError } from '../../core/types.ts' interface SmsTestOpts { configId: string phone?: string @@ -44,7 +45,7 @@ export function buildIntegrationSmsTestCommand(): Command { const configR = await c.get>(`/message/AccountConfig/${opts.configId}`) if (configR.code !== 200 || !configR.data) { const enhanced = enhanceFromBackend(configR.msg || 'id 不存在', 'configId', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) + throw new DacliError(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) } const config = configR.data @@ -62,7 +63,7 @@ export function buildIntegrationSmsTestCommand(): Command { const r = await c.postJson('/message/AccountConfig/testSendSms', body) if (r.code !== 200) { const enhanced = enhanceFromBackend(r.msg || '', 'sms', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) + throw new DacliError(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) } log.info(`✓ 短信测试发送 OK (configId=${opts.configId}, to=${opts.phone})`) cmdSummary = { configId: opts.configId, phone: opts.phone, ok: true } diff --git a/src/commands/integration/webhooktest.ts b/src/commands/integration/webhooktest.ts index 7502ee0..0cd59b2 100644 --- a/src/commands/integration/webhooktest.ts +++ b/src/commands/integration/webhooktest.ts @@ -14,6 +14,7 @@ import { log } from '../../core/log.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 +import { DacliError } from '../../core/types.ts' interface WebhookTestOpts { configId?: string url?: string @@ -41,7 +42,7 @@ export function buildIntegrationWebhookTestCommand(): Command { try { if (!opts.configId && !opts.url) { - throw new Error('必须传 --configId 或 --url 之一') + throw new DacliError('必须传 --configId 或 --url 之一') } let body: Record @@ -51,7 +52,7 @@ export function buildIntegrationWebhookTestCommand(): Command { const configR = await c.get>(`/message/AccountConfig/${opts.configId}`) if (configR.code !== 200 || !configR.data) { const enhanced = enhanceFromBackend(configR.msg || 'id 不存在', 'configId', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) + throw new DacliError(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })) } body = { ...configR.data, @@ -69,14 +70,14 @@ export function buildIntegrationWebhookTestCommand(): Command { try { body = { ...body, ...JSON.parse(opts.payload) } } catch (e) { - throw new Error(`--payload JSON 解析失败: ${(e as Error).message}`) + throw new DacliError(`--payload JSON 解析失败: ${(e as Error).message}`) } } const r = await c.postJson('/message/AccountConfig/testSendWebhook', body) if (r.code !== 200) { const enhanced = enhanceFromBackend(r.msg || '', 'webhook', { entity: 'integration' }) - throw new Error(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) + throw new DacliError(formatErrorLine(enhanced, { step: 1, stepName: '后端API' })) } log.info(`✓ Webhook 测试发送 OK (configId=${opts.configId || '-'}, url=${opts.url || '(from config)'})`) cmdSummary = { diff --git a/src/commands/report/info.ts b/src/commands/report/info.ts index 0cbb78a..5b03426 100644 --- a/src/commands/report/info.ts +++ b/src/commands/report/info.ts @@ -17,6 +17,7 @@ import { log, printJson } from '../../core/log.ts' import { initWebapi as initReportWebapi, loadReport } from '../../webapi/report.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 (info 类查询历史可追溯) +import { DacliError } from '../../core/types.ts' interface ReportInfoItem { id?: string fullName?: string @@ -54,7 +55,7 @@ export function buildReportInfoCommand(): Command { const r = (await loadReport.getReportInfo(opts.id)) as ReportInfoItem | null const found = r if (!found || typeof found !== 'object' || !('id' in found)) { - throw new Error(`report id=${opts.id} 不存在 (webapi /api/Report/${opts.id} 返非 ReportInfoVO 或 null)`) + throw new DacliError(`report id=${opts.id} 不存在 (webapi /api/Report/${opts.id} 返非 ReportInfoVO 或 null)`) } cmdSummary = { id: opts.id, diff --git a/src/commands/report/list.ts b/src/commands/report/list.ts index 591f45d..61161df 100644 --- a/src/commands/report/list.ts +++ b/src/commands/report/list.ts @@ -23,6 +23,7 @@ import { log, printJson } from '../../core/log.ts' import { initWebapi as initReportWebapi, loadReport } from '../../webapi/report.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 (list 类查询历史可追溯) +import { DacliError } from '../../core/types.ts' interface ReportListItem { id?: string fullName?: string @@ -88,7 +89,7 @@ export function buildReportListCommand(): Command { data?: { list?: ReportListItem[]; pagination?: { total?: number } } } if (r.code !== undefined && r.code !== 200) { - throw new Error(`report list 失败 (code=${r.code}): ${r.msg}`) + throw new DacliError(`report list 失败 (code=${r.code}): ${r.msg}`) } const list = r.data?.list || [] const total = r.data?.pagination?.total ?? list.length diff --git a/src/commands/storage/info.ts b/src/commands/storage/info.ts index 4b19fb2..7c65903 100644 --- a/src/commands/storage/info.ts +++ b/src/commands/storage/info.ts @@ -7,6 +7,7 @@ import { log, printJson } from '../../core/log.ts' import { installHelpHook } from '../../core/help.ts' import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 (无 appCode, 跳过 L3 落盘, 仅 L1 summary) +import { DacliError } from '../../core/types.ts' interface InfoOpts { profile?: string yml?: string @@ -35,7 +36,7 @@ export function buildStorageInfoCommand(): Command { const found = cfg.platforms.find((p) => p.name === platform) if (!found) { const avail = cfg.platforms.map((p) => p.name).join(', ') || '(无)' - throw new Error(`platform '${platform}' 不在 yml 中, 可用: ${avail}`) + throw new DacliError(`platform '${platform}' 不在 yml 中, 可用: ${avail}`) } const masked = maskPlatform(found) cmdSummary = { diff --git a/src/commands/user/auth-list.ts b/src/commands/user/auth-list.ts index db210fc..1d6b915 100644 --- a/src/commands/user/auth-list.ts +++ b/src/commands/user/auth-list.ts @@ -11,6 +11,7 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' +import { DacliError } from '../../core/types.ts' interface AuthItem { id?: string fullName?: string @@ -42,7 +43,7 @@ export function buildUserAuthListCommand(): Command { .requiredOption('--objectId ', 'object id (从 auth-groups 拿)') .requiredOption('--objectType ', 'object 类型: role | position | organize', (v) => { if (!['role', 'position', 'organize'].includes(v)) { - throw new Error('objectType 必须是 role/position/organize') + throw new DacliError('objectType 必须是 role/position/organize') } return v }) diff --git a/src/commands/user/batch-state.ts b/src/commands/user/batch-state.ts index ebab61f..61a6f8b 100644 --- a/src/commands/user/batch-state.ts +++ b/src/commands/user/batch-state.ts @@ -10,6 +10,7 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' +import { DacliError } from '../../core/types.ts' interface BatchStateOpts { ids: string state: string @@ -21,7 +22,7 @@ export function buildUserBatchStateCommand(): Command { .requiredOption('--ids ', '用户 id 列表, 逗号分隔') .requiredOption('--state ', '状态: 0=禁用 1=启用 2=锁定', (v) => { const n = parseInt(v, 10) - if (![0, 1, 2].includes(n)) throw new Error('state 必须是 0/1/2') + if (![0, 1, 2].includes(n)) throw new DacliError('state 必须是 0/1/2') return v }) .action(async (opts: BatchStateOpts) => {