Skip to main content

actr_cli/
error.rs

1//! Unified CLI error type system
2//!
3//! Design principles:
4//! 1. Clear semantics: each error type has a well-defined use case
5//! 2. No duplication: eliminate semantically overlapping error types
6//! 3. Layered: distinguish system errors vs. business errors
7//! 4. Easy to debug: provide sufficient context information
8
9use thiserror::Error;
10
11#[derive(Error, Debug)]
12pub enum ActrCliError {
13    // === System-level errors ===
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    // === Configuration errors ===
24    #[error("Configuration error: {0}")]
25    Configuration(String),
26
27    #[error("Invalid project structure: {0}")]
28    InvalidProject(String),
29
30    #[error("Project already exists: {0}")]
31    ProjectExists(String),
32
33    // === Dependency and build errors ===
34    #[error("Dependency resolution failed: {0}")]
35    Dependency(String),
36
37    #[error("Build process failed: {0}")]
38    Build(String),
39
40    #[error("Code generation failed: {0}")]
41    CodeGeneration(String),
42
43    // === Template and initialization errors ===
44    #[error("Template rendering failed: {0}")]
45    Template(#[from] handlebars::RenderError),
46
47    #[error("Unsupported feature: {0}")]
48    Unsupported(String),
49
50    // === Command execution errors ===
51    #[error("Command execution failed: {0}")]
52    Command(String),
53
54    // === Wrapper for underlying library errors ===
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    // === Generic error wrapper ===
65    #[error("Internal error: {0}")]
66    Internal(#[from] anyhow::Error),
67}
68
69// Error type conversion helpers
70impl ActrCliError {
71    /// Convert a string into a configuration error
72    pub fn config_error(msg: impl Into<String>) -> Self {
73        Self::Configuration(msg.into())
74    }
75
76    /// Convert a string into a dependency error
77    pub fn dependency_error(msg: impl Into<String>) -> Self {
78        Self::Dependency(msg.into())
79    }
80
81    /// Convert a string into a build error
82    pub fn build_error(msg: impl Into<String>) -> Self {
83        Self::Build(msg.into())
84    }
85
86    /// Convert a string into a command execution error
87    pub fn command_error(msg: impl Into<String>) -> Self {
88        Self::Command(msg.into())
89    }
90
91    /// Check whether this is a configuration-related error
92    pub fn is_config_error(&self) -> bool {
93        matches!(
94            self,
95            Self::Configuration(_) | Self::ConfigParsing(_) | Self::InvalidProject(_)
96        )
97    }
98
99    /// Check whether this is a network-related error
100    pub fn is_network_error(&self) -> bool {
101        matches!(self, Self::Network(_))
102    }
103
104    /// Get a user-friendly hint for this error
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 manifest.toml configuration file"),
110            Self::Dependency(_) => {
111                Some("💡 Try 'actr deps install --force' to refresh dependencies")
112            }
113            Self::Build(_) => Some("💡 Check proto files and dependencies"),
114            Self::Network(_) => Some("💡 Check your network connection and proxy settings"),
115            Self::Unsupported(_) => Some("💡 This feature is not implemented yet"),
116            _ => None,
117        }
118    }
119}
120
121/// CLI-specific Result type
122pub type Result<T> = std::result::Result<T, ActrCliError>;
123
124// === Error compatibility conversions ===
125// Ensure backward compatibility while guiding migration to new error types