1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, DoumError>;
5
6#[derive(Error, Debug)]
8pub enum DoumError {
9 #[error("Configuration error: {0}")]
11 Config(String),
12
13 #[error("LLM API error: {0}")]
15 LLM(String),
16
17 #[error("Parse error: {0}")]
19 Parse(String),
20
21 #[error("IO error: {0}")]
23 Io(#[from] std::io::Error),
24
25 #[error("Command execution failed: {0}")]
27 CommandExecution(String),
28
29 #[error("User cancelled operation")]
31 UserCancelled,
32
33 #[error("Invalid configuration: {0}")]
35 InvalidConfig(String),
36
37 #[error("Request timeout")]
39 Timeout,
40
41 #[error("HTTP request error: {0}")]
43 Reqwest(#[from] reqwest::Error),
44
45 #[error("JSON error: {0}")]
47 Json(#[from] serde_json::Error),
48
49 #[error("TOML error: {0}")]
51 Toml(#[from] toml::de::Error),
52
53 #[error("TOML serialization error: {0}")]
55 TomlSer(#[from] toml::ser::Error),
56}
57
58impl DoumError {
59 pub fn user_message(&self) -> String {
61 match self {
62 DoumError::Config(msg) => {
63 format!(
64 "⚙️ Configuration Error\n\n\
65 Problem: {}\n\n\
66 💡 Solution:\n\
67 1. Check your config file location:\n\
68 • Windows: %APPDATA%\\doum-cli\\config.toml\n\
69 • macOS: ~/Library/Application Support/doum-cli/config.toml\n\
70 • Linux: ~/.config/doum-cli/config.toml\n\
71 2. Reset to default: doum config unset <key>\n\
72 3. View all settings: doum config show",
73 msg
74 )
75 }
76 DoumError::LLM(msg) => {
77 if msg.contains("401") || msg.contains("unauthorized") {
78 "🔑 API Key Error\n\n\
79 Problem: Invalid or missing API key\n\n\
80 💡 Solution:\n\
81 1. Set your API key: doum config set llm.api_key sk-...\n\
82 2. Get a key from: https://platform.openai.com/api-keys\n\
83 3. Verify key format (starts with 'sk-')".to_string()
84 } else if msg.contains("timeout") || msg.contains("timed out") {
85 "⏱️ Request Timeout\n\n\
86 Problem: LLM request took too long\n\n\
87 💡 Solution:\n\
88 1. Increase timeout: doum config set llm.timeout 60\n\
89 2. Check your internet connection\n\
90 3. Try again in a few moments".to_string()
91 } else if msg.contains("rate limit") || msg.contains("429") {
92 "🚦 Rate Limit Exceeded\n\n\
93 Problem: Too many requests to the API\n\n\
94 💡 Solution:\n\
95 1. Wait a moment and try again\n\
96 2. Check your API quota at: https://platform.openai.com/usage\n\
97 3. Consider upgrading your plan".to_string()
98 } else {
99 format!(
100 "🤖 LLM API Error\n\n\
101 Problem: {}\n\n\
102 💡 Solution:\n\
103 1. Check your internet connection\n\
104 2. Verify API key: doum config get llm.api_key\n\
105 3. Check OpenAI status: https://status.openai.com",
106 msg
107 )
108 }
109 }
110 DoumError::Parse(msg) => {
111 format!(
112 "📝 Parse Error\n\n\
113 Problem: Failed to parse LLM response\n\
114 Details: {}\n\n\
115 💡 Solution:\n\
116 1. This usually resolves automatically (retry logic active)\n\
117 2. If it persists, try a different model: doum config set llm.model gpt-4\n\
118 3. Increase retry limit: doum config set llm.max_retries 5",
119 msg
120 )
121 }
122 DoumError::CommandExecution(msg) => {
123 format!(
124 "⚠️ Command Execution Failed\n\n\
125 Problem: {}\n\n\
126 💡 Solution:\n\
127 1. Check if you have necessary permissions\n\
128 2. Verify the command is valid for your OS/shell\n\
129 3. Try running the command manually first\n\
130 4. Use suggest mode to explore alternatives: doum suggest \"<request>\"",
131 msg
132 )
133 }
134 DoumError::UserCancelled => {
135 "❌ Operation Cancelled\n\n\
136 You cancelled the operation. No changes were made.".to_string()
137 }
138 DoumError::InvalidConfig(msg) => {
139 format!(
140 "🔧 Invalid Configuration\n\n\
141 Problem: {}\n\n\
142 💡 Solution:\n\
143 1. View current config: doum config show\n\
144 2. Reset to default: doum config unset <key>\n\
145 3. Check valid values in documentation",
146 msg
147 )
148 }
149 DoumError::Timeout => {
150 "⏱️ Request Timeout\n\n\
151 Problem: The request took too long\n\n\
152 💡 Solution:\n\
153 1. Increase timeout: doum config set llm.timeout 60\n\
154 2. Check your internet connection\n\
155 3. Try with a simpler request".to_string()
156 }
157 DoumError::Io(err) => {
158 format!(
159 "💾 File System Error\n\n\
160 Problem: {}\n\n\
161 💡 Solution:\n\
162 1. Check file permissions\n\
163 2. Verify the path exists\n\
164 3. Make sure you have sufficient disk space",
165 err
166 )
167 }
168 DoumError::Reqwest(err) => {
169 if err.is_timeout() {
170 "⏱️ Network Timeout\n\n\
171 Problem: Network request timed out\n\n\
172 💡 Solution:\n\
173 1. Check your internet connection\n\
174 2. Increase timeout: doum config set llm.timeout 60\n\
175 3. Try again in a few moments".to_string()
176 } else if err.is_connect() {
177 "🌐 Connection Error\n\n\
178 Problem: Failed to connect to the API\n\n\
179 💡 Solution:\n\
180 1. Check your internet connection\n\
181 2. Verify firewall settings\n\
182 3. Check if you need a proxy".to_string()
183 } else {
184 format!(
185 "🌐 Network Error\n\n\
186 Problem: {}\n\n\
187 💡 Solution:\n\
188 1. Check your internet connection\n\
189 2. Try again in a few moments",
190 err
191 )
192 }
193 }
194 DoumError::Json(err) => {
195 format!(
196 "📄 JSON Error\n\n\
197 Problem: Failed to parse JSON data\n\
198 Details: {}\n\n\
199 💡 This is likely a temporary issue. Please try again.",
200 err
201 )
202 }
203 DoumError::Toml(err) => {
204 format!(
205 "📝 Configuration File Error\n\n\
206 Problem: Failed to read config file\n\
207 Details: {}\n\n\
208 💡 Solution:\n\
209 1. Check if config file is corrupted\n\
210 2. Backup and delete config file to reset\n\
211 3. Config will be recreated with defaults",
212 err
213 )
214 }
215 DoumError::TomlSer(err) => {
216 format!(
217 "📝 Configuration Save Error\n\n\
218 Problem: Failed to save config file\n\
219 Details: {}\n\n\
220 💡 Solution:\n\
221 1. Check file permissions\n\
222 2. Verify disk space is available",
223 err
224 )
225 }
226 }
227 }
228}