Files
seal-use/src/core/errors.ts
T
yunyingk c572cba842 fix(approval-runs): 传输层失败不再被误判为业务结论
`sealApprovalRunFlowGet` 用两次"请求被拒"充当路由信号:记录查询被拒,就
认为入参是单号;运行记录列表被拒,就认为该单据没有 AI 记录。两处都写成
裸 `catch {}`,于是所有失败都被吸收进这两个业务结论。

后果不是偶尔不准,而是以十足的语气给出错误答案:token 过期、连接重置、
网关 5xx 会被渲染成「单据已删除」或「AI 从未审核」,用户没有任何线索去
怀疑刚才那次请求其实失败了;退出码同时失去语义,本该由 classifyCliError
产出 11(认证/上游失败),实际退化成一次"成功的查询"。

现在只有标识符形态的拒绝才计为未命中:HTTP 400/404/422,或响应体确实
到达但不是一条审批记录。认证失败、5xx、限流、以及根本没有响应的传输
错误一律原样上抛。

- errors:导出既有的 `responseStatus` 结构读取器,让调用方能区分"没有
  响应"与"收到了 400/404 响应"。
- approval-runs:新增 `isUpstreamLookupMiss`,替换两处裸 catch。
- test:新增四个回归用例(401/403/503/连接失败),断言原始错误对象抵达
  调用方;另加一条非合思租户的守卫用例。
2026-11-10 19:47:48 +08:00

113 lines
4.0 KiB
TypeScript

export type CliExitCode = 1 | 2 | 10 | 11 | 12 | 13 | 14;
export class CliError extends Error {
readonly exitCode: CliExitCode;
readonly nextAction?: string;
constructor(message: string, exitCode: CliExitCode, nextAction?: string) {
super(message);
this.name = "CliError";
this.exitCode = exitCode;
this.nextAction = nextAction;
}
}
export class PayloadTooLargeError extends CliError {
constructor(message: string, nextAction?: string) {
super(message, 12, nextAction);
this.name = "PayloadTooLargeError";
}
}
export function usageError(message: string, nextAction?: string): CliError {
return new CliError(message, 2, nextAction);
}
/**
* Local configuration/argument validation failure: the CLI was handed a value
* it cannot use (bad `--env` value, unparseable enterprise file, missing
* required parameter). Distinct from `2` (malformed command line) so callers
* can tell "your command line is wrong" from "your config/input data is wrong".
*/
export function validationError(message: string, nextAction?: string): CliError {
return new CliError(message, 13, nextAction);
}
/**
* Safety gate: the requested action is blocked by an environment guard rather
* than by the caller's input — replaying into production, running a regression
* matrix outside a test tenant. Passing the guard requires changing target or
* setting an explicit override flag.
*/
export function safetyBlocked(message: string, nextAction?: string): CliError {
return new CliError(message, 14, nextAction);
}
export function confirmationRequired(message: string, nextAction: string): CliError {
return new CliError(message, 10, nextAction);
}
/**
* Malformed command line: the invocation itself is wrong (unknown subcommand,
* bad flag value, missing positional). Exit `2` — the caller must retype.
*/
const USAGE_PREFIX =
/^(?:Usage:|Unknown (?:[a-z0-9_-]+ )*(?:command|action|subcommand|target)|Invalid (?:number|boolean|command)|--json must|Missing required JSON field|Invalid JSON:)/i;
export function classifyCliError(error: unknown): {
exitCode: CliExitCode;
nextAction?: string;
} {
if (error instanceof CliError) {
return { exitCode: error.exitCode, nextAction: error.nextAction };
}
const value = error instanceof Error ? error.message : String(error);
const status = responseStatus(error);
if (status === 401 || status === 403 || /(?:^|\s)(?:token|bearer|session) (?:expired|missing|invalid)/i.test(value)) {
return { exitCode: 11 };
}
if (USAGE_PREFIX.test(value)) {
return { exitCode: 2 };
}
// Structural validation: a ZodError carries an `issues` array rather than a
// message prefix, so it never matches a text pattern.
if (isValidationIssueList(error)) {
return { exitCode: 13 };
}
// JSON.parse failures (unquoted config, truncated file) are SyntaxErrors with
// no stable English wording across runtimes ("JSON Parse error: ..." under
// Bun, "Unexpected token ..." under Node).
if (error instanceof SyntaxError) {
return { exitCode: 13 };
}
return { exitCode: 1 };
}
function isValidationIssueList(error: unknown): boolean {
if (!error || typeof error !== "object") return false;
const issues = (error as { issues?: unknown }).issues;
if (!Array.isArray(issues)) return false;
return issues.every(
(issue) =>
issue &&
typeof issue === "object" &&
"path" in issue &&
"message" in issue
);
}
/**
* Read the HTTP status off an error thrown by the HTTP layer, structurally:
* ky's `HTTPError` is not a dependency of this module, and test doubles only
* carry `{ response: { status } }`. `undefined` means the request never got a
* response (DNS failure, connection reset, timeout).
*/
export function responseStatus(error: unknown): number | undefined {
if (!error || typeof error !== "object") return undefined;
const response = (error as { response?: unknown }).response;
if (!response || typeof response !== "object") return undefined;
const status = (response as { status?: unknown }).status;
return typeof status === "number" ? status : undefined;
}