actr_cli/
error.rs

1//! 统一的CLI错误类型系统
2//!
3//! 设计原则:
4//! 1. 语义明确:每种错误类型都有明确的使用场景
5//! 2. 避免重复:消除语义重叠的错误类型
6//! 3. 层次分明:区分系统错误vs业务错误
7//! 4. 易于调试:提供足够的上下文信息
8
9use thiserror::Error;
10
11#[derive(Error, Debug)]
12pub enum ActrCliError {
13    // === 系统级错误 ===
14    #[error("IO operation failed: {0}")]
15    Io(#[from] std::io::Error),
16
17    #[error("Network request failed: {0}")]
18    Network(#[from] reqwest::Error),
19
20    #[error("JSON serialization failed: {0}")]
21    Serialization(#[from] serde_json::Error),
22
23    #[error("Git operation failed: {0}")]
24    Git(#[from] git2::Error),
25
26    // === 配置相关错误 ===
27    #[error("Configuration error: {0}")]
28    Configuration(String),
29
30    #[error("Invalid project structure: {0}")]
31    InvalidProject(String),
32
33    #[error("Project already exists: {0}")]
34    ProjectExists(String),
35
36    // === 依赖和构建错误 ===
37    #[error("Dependency resolution failed: {0}")]
38    Dependency(String),
39
40    #[error("Build process failed: {0}")]
41    Build(String),
42
43    #[error("Code generation failed: {0}")]
44    CodeGeneration(String),
45
46    // === 模板和初始化错误 ===
47    #[error("Template rendering failed: {0}")]
48    Template(#[from] handlebars::RenderError),
49
50    // === 命令执行错误 ===
51    #[error("Command execution failed: {0}")]
52    Command(String),
53
54    // === 底层库错误的包装 ===
55    #[error("Actor framework error: {0}")]
56    Actor(#[from] actr_protocol::ActrError),
57
58    #[error("URI parsing error: {0}")]
59    UriParsing(#[from] actr_protocol::uri::ActrUriError),
60
61    #[error("Configuration parsing error: {0}")]
62    ConfigParsing(#[from] actr_config::ConfigError),
63
64    // === 通用错误包装器 ===
65    #[error("Internal error: {0}")]
66    Internal(#[from] anyhow::Error),
67}
68
69// 错误类型转换辅助
70impl ActrCliError {
71    /// 将字符串转换为配置错误
72    pub fn config_error(msg: impl Into<String>) -> Self {
73        Self::Configuration(msg.into())
74    }
75
76    /// 将字符串转换为依赖错误
77    pub fn dependency_error(msg: impl Into<String>) -> Self {
78        Self::Dependency(msg.into())
79    }
80
81    /// 将字符串转换为构建错误
82    pub fn build_error(msg: impl Into<String>) -> Self {
83        Self::Build(msg.into())
84    }
85
86    /// 将字符串转换为命令执行错误
87    pub fn command_error(msg: impl Into<String>) -> Self {
88        Self::Command(msg.into())
89    }
90
91    /// 检查是否为配置相关错误
92    pub fn is_config_error(&self) -> bool {
93        matches!(
94            self,
95            Self::Configuration(_) | Self::ConfigParsing(_) | Self::InvalidProject(_)
96        )
97    }
98
99    /// 检查是否为网络相关错误
100    pub fn is_network_error(&self) -> bool {
101        matches!(self, Self::Network(_))
102    }
103
104    /// 获取用户友好的错误提示
105    pub fn user_hint(&self) -> Option<&str> {
106        match self {
107            Self::InvalidProject(_) => Some("💡 Use 'actr init' to initialize a new project"),
108            Self::ProjectExists(_) => Some("💡 Use --force to overwrite existing project"),
109            Self::Configuration(_) => Some("💡 Check your Actr.toml configuration file"),
110            Self::Dependency(_) => Some("💡 Try 'actr install --force' to refresh dependencies"),
111            Self::Build(_) => Some("💡 Check proto files and dependencies"),
112            Self::Network(_) => Some("💡 Check your network connection and proxy settings"),
113            _ => None,
114        }
115    }
116}
117
118/// CLI特定的Result类型
119pub type Result<T> = std::result::Result<T, ActrCliError>;
120
121// === 错误兼容性转换 ===
122// 保证现有代码的兼容性,同时引导向新错误类型迁移