Add exit codes 13 and 14 for config validation and safety blocks
The classifier only matched English usage wording, so every Chinese-language
usage error, every enterprise-selection failure, and every Zod/SyntaxError
fell through to exit 1 ("upstream failure"), while environment safety guards
were reported as exit 2 or 1. Add two semantic codes:
- 13 (validationError): well-formed command, unusable value — missing --corp,
unparseable enterprise JSON, missing required parameter, ambiguous webhook
channel selection.
- 14 (safetyBlocked): the target environment forbids the action — replaying
into production, regression outside a test tenant.
classifyCliError now recognises Chinese usage wording, enterprise-selection
phrasing, local file problems, Zod issue lists (structural, not textual) and
SyntaxError; English Usage:/Unknown-command stays 2, domain failures and
quality gates stay 1. Docs (AGENTS, cli-protocol, skill cli.md) and the exit
code assertions follow.
This commit is contained in:
1 parent
5f4906758e
commit
66eb81f3d1
12 files changed
+177
-134
No files matched your search
@@ -387,7 +387,9 @@ describe("seal-use webhook replay CLI", () => {
|
||||
"--dry-run"
|
||||
]);
|
||||
expect(blockedResult.isError).toBe(true);
|
||||
expect(blockedResult.exitCode).toBe(1);
|
||||
// 14: safety block — the target environment forbids the action, not the
|
||||
// shape of the command line.
|
||||
expect(blockedResult.exitCode).toBe(14);
|
||||
expect(blockedResult.stderr).toContain("标记为生产正式环境");
|
||||
|
||||
// With --force
|
||||
|
||||
@@ -6,7 +6,7 @@ import { existsSync } from "node:fs";
|
||||
import { runTool, selectCorp } from "../helpers.js";
|
||||
import * as api from "../../domains/seal/api.js";
|
||||
import { findWebhookIntegration } from "../../domains/seal/api.js";
|
||||
import type { ApprovalRun, WebhookIntegration } from "../../domains/seal/types.js";
|
||||
import type { WebhookIntegration } from "../../domains/seal/types.js";
|
||||
import {
|
||||
executeApprovalRunsReplay,
|
||||
resolveReplayCorpPair,
|
||||
@@ -31,7 +31,7 @@ import {
|
||||
import { outputOptions, printJson } from "../printer.js";
|
||||
import type { ParsedArgs } from "../types.js";
|
||||
import type { MaterialPackFilePolicyPatch } from "../../domains/seal/material-pack/policy.js";
|
||||
import { CliError, confirmationRequired, usageError } from "../errors.js";
|
||||
import { CliError, confirmationRequired, usageError, validationError } from "../errors.js";
|
||||
|
||||
export async function handleWebhookResourcePackPreview(
|
||||
filePath: string,
|
||||
@@ -295,7 +295,7 @@ export async function handleWebhookCommand(
|
||||
console.error(`👉 自动锁定唯一可用 Webhook 通道: ${integration.name} (${integration.id})`);
|
||||
} else if (integrations.length > 1) {
|
||||
const options = integrations.map((i) => `"${i.name}" (${i.id})`).join(", ");
|
||||
throw new Error(`当前租户存在多个 Webhook 通道 [${options}],请通过 --webhook <idOrName> 指定目标通道。`);
|
||||
throw validationError(`当前租户存在多个 Webhook 通道 [${options}],请通过 --webhook <idOrName> 指定目标通道。`);
|
||||
} else {
|
||||
throw new CliError(
|
||||
"当前租户未配置任何 Webhook 集成通道。",
|
||||
|
||||
+66
-1
@@ -1,5 +1,12 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CliError, classifyCliError, confirmationRequired, PayloadTooLargeError } from "./errors.js";
|
||||
import {
|
||||
CliError,
|
||||
classifyCliError,
|
||||
confirmationRequired,
|
||||
PayloadTooLargeError,
|
||||
safetyBlocked,
|
||||
validationError
|
||||
} from "./errors.js";
|
||||
|
||||
describe("CLI semantic errors", () => {
|
||||
test("preserves explicit exit codes and next actions", () => {
|
||||
@@ -33,5 +40,63 @@ describe("CLI semantic errors", () => {
|
||||
expect(classifyCliError(new Error("Unknown billing command: invalid"))).toEqual({ exitCode: 2 });
|
||||
expect(classifyCliError(new Error("Unknown webhook command: invalid"))).toEqual({ exitCode: 2 });
|
||||
expect(classifyCliError(new Error("Usage: seal-use contacts children <departmentId>"))).toEqual({ exitCode: 2 });
|
||||
expect(classifyCliError(new Error("Invalid boolean: now"))).toEqual({ exitCode: 2 });
|
||||
expect(classifyCliError(new Error("Unknown deploy hosecloud action: x"))).toEqual({ exitCode: 2 });
|
||||
});
|
||||
|
||||
test("classifies local config and input-data failures as validation errors (13)", () => {
|
||||
// Chinese-language usage wording: the message names a flag/parameter to supply.
|
||||
expect(
|
||||
classifyCliError(new Error("当前租户存在多个 Webhook 通道 [\"a\" (1)],请通过 --webhook <idOrName> 指定目标通道。"))
|
||||
).toEqual({ exitCode: 13 });
|
||||
expect(classifyCliError(new Error("必须提供 target、recordId 或 sourceDocumentSN 参数。"))).toEqual({ exitCode: 13 });
|
||||
|
||||
// Enterprise selection: well-formed command, incomplete local config.
|
||||
expect(
|
||||
classifyCliError(new Error("Stateless mode is enabled. Every enterprise command requires --corp <corpId>."))
|
||||
).toEqual({ exitCode: 13 });
|
||||
expect(classifyCliError(new Error("No enterprise config found for nope"))).toEqual({ exitCode: 13 });
|
||||
expect(
|
||||
classifyCliError(new Error("rules lint requires an authenticated enterprise. Run 'seal-use corps list' to select one."))
|
||||
).toEqual({ exitCode: 13 });
|
||||
|
||||
// Local file input the caller must fix.
|
||||
expect(classifyCliError(new Error("File not found: /tmp/missing.json"))).toEqual({ exitCode: 13 });
|
||||
expect(
|
||||
classifyCliError(new Error("本地规则文件格式错误: 顶层必须为规则数组或包含 'rules' 数组的对象"))
|
||||
).toEqual({ exitCode: 13 });
|
||||
|
||||
// Structural signals that carry no message prefix.
|
||||
expect(classifyCliError(new SyntaxError("JSON Parse error: Expected '}'"))).toEqual({ exitCode: 13 });
|
||||
expect(classifyCliError({ issues: [{ path: ["environment"], message: "Invalid option" }] })).toEqual({ exitCode: 13 });
|
||||
});
|
||||
|
||||
test("classifies safety-guard blocks distinctly (14)", () => {
|
||||
expect(
|
||||
classifyCliError(new Error("目标企业「生产目标企业」(prod) 标记为生产正式环境 (production)。"))
|
||||
).toEqual({ exitCode: 14 });
|
||||
expect(classifyCliError(new Error("重放目标企业「T」(t) 的环境为「production」,非测试环境!"))).toEqual({
|
||||
exitCode: 14
|
||||
});
|
||||
expect(classifyCliError(safetyBlocked("blocked by environment guard", "seal-use webhook replay --force"))).toEqual({
|
||||
exitCode: 14,
|
||||
nextAction: "seal-use webhook replay --force"
|
||||
});
|
||||
});
|
||||
|
||||
test("validationError helper carries exit 13 with next action", () => {
|
||||
expect(classifyCliError(validationError("请选择目标通道", "seal-use webhook list"))).toEqual({
|
||||
exitCode: 13,
|
||||
nextAction: "seal-use webhook list"
|
||||
});
|
||||
});
|
||||
|
||||
test("keeps domain failures and quality gates at exit 1", () => {
|
||||
expect(classifyCliError(new Error("未找到指定的规则版本: 99"))).toEqual({ exitCode: 1 });
|
||||
expect(classifyCliError(new Error("[Linter 质量门禁未通过] 综合评分 80 低于要求的最低分 85 分"))).toEqual({
|
||||
exitCode: 1
|
||||
});
|
||||
expect(classifyCliError(new Error("Webhook 单据推送失败 (500): internal"))).toEqual({ exitCode: 1 });
|
||||
expect(classifyCliError(new Error("测试企业「T」AI 审批额度不足:当前剩余 200 点"))).toEqual({ exitCode: 1 });
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,8 @@ export {
|
||||
CliError,
|
||||
PayloadTooLargeError,
|
||||
usageError,
|
||||
validationError,
|
||||
safetyBlocked,
|
||||
confirmationRequired,
|
||||
classifyCliError
|
||||
} from "../core/errors.js";
|
||||
Reference in new issue
Block a user