refactor(errors): unify EkbError hierarchy across domains and structured CLI error classification
This commit is contained in:
1 parent
2b220bc5d9
commit
c15d622ba2
8 files changed
+89
-40
No files matched your search
@@ -5,6 +5,7 @@ import {
|
||||
getCorpStaffId,
|
||||
getCorpStaffName,
|
||||
} from "./types.js";
|
||||
import { NotFoundError, NotLoggedInError, ValidationError } from "../errors.js";
|
||||
import { saveGlobalConfig } from "./loader.js";
|
||||
|
||||
export interface CorpSummary {
|
||||
@@ -44,11 +45,11 @@ export function resolveCorpConfig(
|
||||
corp = corps.find((item) => getCorpPhysicalId(item) === selectedCorpId);
|
||||
}
|
||||
if (corp) return corp;
|
||||
throw new Error(`未找到企业或身份标识为 [${selectedCorpId}] 的企业配置`);
|
||||
throw new NotFoundError(`未找到企业或身份标识为 [${selectedCorpId}] 的企业配置`);
|
||||
}
|
||||
|
||||
if (!options.stateful) {
|
||||
throw new Error(
|
||||
throw new ValidationError(
|
||||
"当前处于无状态模式 (Stateless Mode)。执行企业级操作必须显式指定 --corp <corpId>。\n" +
|
||||
"如需设置默认企业,可执行 `hose-use config mode stateful` 切换为有状态模式。"
|
||||
);
|
||||
@@ -58,7 +59,7 @@ export function resolveCorpConfig(
|
||||
if (firstCorp) return firstCorp;
|
||||
|
||||
const hintDirs = candidateDirs?.length ? ` (已检索路径: ${candidateDirs.join(", ")})` : "";
|
||||
throw new Error(`未找到任何企业配置。请先运行 'hose-use auth login' 登录或添加企业配置${hintDirs}。`);
|
||||
throw new NotLoggedInError(`未找到任何企业配置。请先运行 'hose-use auth login' 登录或添加企业配置${hintDirs}。`);
|
||||
}
|
||||
|
||||
export class CorpContext {
|
||||
|
||||
@@ -12,12 +12,35 @@ export class EkbError extends Error {
|
||||
export class NotLoggedInError extends EkbError {
|
||||
constructor(message = "No corp selected") {
|
||||
super(message, "NOT_LOGGED_IN", 401);
|
||||
this.name = "NotLoggedInError";
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthenticationError extends EkbError {
|
||||
constructor(message = "Authentication failed") {
|
||||
super(message, "AUTH_FAILED", 401);
|
||||
this.name = "AuthenticationError";
|
||||
}
|
||||
}
|
||||
|
||||
export class PermissionError extends EkbError {
|
||||
constructor(message = "Permission denied") {
|
||||
super(message, "FORBIDDEN", 403);
|
||||
this.name = "PermissionError";
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends EkbError {
|
||||
constructor(message: string, code = "NOT_FOUND") {
|
||||
super(message, code, 404);
|
||||
this.name = "NotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
export class ValidationError extends EkbError {
|
||||
constructor(message: string, code = "VALIDATION_ERROR") {
|
||||
super(message, code, 400);
|
||||
this.name = "ValidationError";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,3 +58,4 @@ export class EkbApiError extends EkbError {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user