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; }