/** * 自定义错误类型 * 用于 CLI 工具的错误处理 */ /** * 模板未找到错误 */ export class TemplateNotFoundError extends Error { public readonly templateName: string; constructor(templateName: string) { super(`模板 '${templateName}' 不存在,请选择有效的模板`); this.name = 'TemplateNotFoundError'; this.templateName = templateName; } } /** * 文件系统错误 */ export class FileSystemError extends Error { public readonly originalError?: Error; constructor(message: string, originalError?: Error) { super(message); this.name = 'FileSystemError'; this.originalError = originalError; } } /** * 配置错误(如模板文件夹缺失) */ export class ConfigurationError extends Error { constructor(message: string) { super(message); this.name = 'ConfigurationError'; } } /** * 规则文件未找到错误 */ export class RulesNotFoundError extends Error { public readonly templateName: string; constructor(templateName: string) { super(`模板 '${templateName}' 中没有规则文件`); this.name = 'RulesNotFoundError'; this.templateName = templateName; } } /** * 根据错误类型获取用户友好的错误消息 * @param error 错误对象 * @returns 用户友好的错误消息 */ export function getErrorMessage(error: unknown): string { if (error instanceof TemplateNotFoundError) { return error.message; } if (error instanceof FileSystemError) { return error.message; } if (error instanceof ConfigurationError) { return error.message; } if (error instanceof RulesNotFoundError) { return error.message; } if (error instanceof Error) { return error.message; } return '发生未知错误'; }