feat(dacli): P5 sample 9 文件 throw new Error → DacliError

命令路径 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 都正常
This commit is contained in:
ssw committed 2026-07-21 17:45:54 +08:00
1 parent 22593df7c4
commit 45e9f1053a
9 files changed
+24 -15

No files matched your search

+3 -2
View File
@@ -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 }
+3 -2
View File
@@ -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<Record<string, unknown>>(`/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 }
+3 -2
View File
@@ -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<Record<string, unknown>>(`/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 }
+5 -4
View File
@@ -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<string, unknown>
@@ -51,7 +52,7 @@ export function buildIntegrationWebhookTestCommand(): Command {
const configR = await c.get<Record<string, unknown>>(`/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 = {
+2 -1
View File
@@ -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,
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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 = {
+2 -1
View File
@@ -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 <id>', 'object id (从 auth-groups 拿)')
.requiredOption('--objectType <t>', '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
})
+2 -1
View File
@@ -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 <list>', '用户 id 列表, 逗号分隔')
.requiredOption('--state <n>', '状态: 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) => {