1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
6pub enum WinxError {
7 #[error("Failed to initialize shell: {0}")]
9 ShellInitializationError(String),
10
11 #[error("Workspace path error: {0}")]
13 WorkspacePathError(String),
14
15 #[error("Failed to lock the bash state: {0}")]
17 BashStateLockError(String),
18
19 #[error("Bash state not initialized. Please call Initialize first with type=\"first_call\" and a valid workspace path.")]
21 BashStateNotInitialized,
22
23 #[error("Command execution failed: {0}")]
25 CommandExecutionError(String),
26
27 #[error("Failed to parse arguments: {0}")]
29 ArgumentParseError(String),
30
31 #[error("File access error for {path}: {message}")]
33 FileAccessError { path: PathBuf, message: String },
34
35 #[error("Security violation: {message}")]
37 PathSecurityError { path: PathBuf, message: String },
38
39 #[error("Command not allowed: {0}")]
41 CommandNotAllowed(String),
42
43 #[error("Thread ID mismatch: {0}")]
45 ThreadIdMismatch(String),
46
47 #[error("Deserialization error: {0}")]
49 DeserializationError(String),
50
51 #[error("Serialization error: {0}")]
53 SerializationError(String),
54
55 #[error("Search/replace syntax error: {0}")]
57 SearchReplaceSyntaxError(String),
58
59 #[error("Search block not found in content: {0}")]
61 SearchBlockNotFound(String),
62
63 #[error("Search block matched multiple times")]
65 SearchBlockAmbiguous { block_content: String, match_count: usize, suggestions: Vec<String> },
66
67 #[error("Search/replace syntax error: {message}")]
69 SearchReplaceSyntaxErrorDetailed {
70 message: String,
71 line_number: Option<usize>,
72 block_type: Option<String>,
73 suggestions: Vec<String>,
74 },
75
76 #[error("Invalid JSON: {0}")]
78 JsonParseError(String),
79
80 #[error("File {path} is too large: {size} bytes (max {max_size})")]
82 FileTooLarge { path: PathBuf, size: u64, max_size: u64 },
83
84 #[error("Failed to write file {path}: {message}")]
86 FileWriteError { path: PathBuf, message: String },
87
88 #[error("Failed to load data: {0}")]
90 DataLoadingError(String),
91
92 #[error("Invalid parameter: {field} - {message}")]
94 ParameterValidationError { field: String, message: String },
95
96 #[error("Required parameter missing: {field} - {message}")]
98 MissingParameterError { field: String, message: String },
99
100 #[error("Null or undefined value where object expected: {field}")]
102 NullValueError { field: String },
103
104 #[error("{message} - {suggestion}")]
106 RecoverableSuggestionError { message: String, suggestion: String },
107
108 #[error("Context save error: {0}")]
110 ContextSaveError(String),
111
112 #[error("Command timed out after {timeout_seconds}s: {command}")]
114 CommandTimeout { command: String, timeout_seconds: u64 },
115
116 #[error(
118 "Interactive command detected: {command}. Use appropriate flags or consider alternatives."
119 )]
120 InteractiveCommandDetected { command: String },
121
122 #[error("A command is already running: '{current_command}' (for {duration_seconds:.1}s). Use status_check, send_text, or interrupt.")]
124 CommandAlreadyRunning { current_command: String, duration_seconds: f64 },
125
126 #[error("Failed to cleanup process: {message}")]
128 ProcessCleanupError { message: String },
129
130 #[error("Command output exceeded maximum size: {size} bytes (max {max_size})")]
132 BufferOverflow { size: usize, max_size: usize },
133
134 #[error("Failed to recover bash session: {message}")]
136 SessionRecoveryError { message: String },
137
138 #[error("Resource allocation failed: {message}")]
140 ResourceAllocationError { message: String },
141
142 #[error("IO error: {0}")]
144 IoError(#[from] std::io::Error),
145
146 #[error("Configuration error: {0}")]
148 ConfigurationError(String),
149
150 #[error("Parse error: {0}")]
152 ParseError(String),
153
154 #[error("Invalid input: {0}")]
156 InvalidInput(String),
157
158 #[error("File error: {0}")]
160 FileError(String),
161}
162
163pub type Result<T> = std::result::Result<T, WinxError>;
165
166impl From<anyhow::Error> for WinxError {
168 fn from(error: anyhow::Error) -> Self {
169 WinxError::CommandExecutionError(format!("{error:#}"))
173 }
174}
175
176pub struct ErrorRecovery;
178
179impl ErrorRecovery {
180 pub fn suggest(error: WinxError, _suggestion: &str) -> WinxError {
181 error
182 }
183
184 pub fn param_error(field: &str, message: &str) -> WinxError {
185 WinxError::ParameterValidationError {
186 field: field.to_string(),
187 message: message.to_string(),
188 }
189 }
190
191 pub fn missing_param(field: &str, message: &str) -> WinxError {
192 WinxError::MissingParameterError { field: field.to_string(), message: message.to_string() }
193 }
194
195 pub fn null_value(field: &str) -> WinxError {
196 WinxError::NullValueError { field: field.to_string() }
197 }
198}
199
200impl Clone for WinxError {
202 fn clone(&self) -> Self {
203 match self {
204 Self::ShellInitializationError(msg) => Self::ShellInitializationError(msg.clone()),
205 Self::WorkspacePathError(msg) => Self::WorkspacePathError(msg.clone()),
206 Self::BashStateLockError(msg) => Self::BashStateLockError(msg.clone()),
207 Self::BashStateNotInitialized => Self::BashStateNotInitialized,
208 Self::CommandExecutionError(msg) => Self::CommandExecutionError(msg.clone()),
209 Self::CommandNotAllowed(msg) => Self::CommandNotAllowed(msg.clone()),
210 Self::ThreadIdMismatch(msg) => Self::ThreadIdMismatch(msg.clone()),
211 Self::ArgumentParseError(msg) => Self::ArgumentParseError(msg.clone()),
212 Self::FileAccessError { path, message } => {
213 Self::FileAccessError { path: path.clone(), message: message.clone() }
214 }
215 Self::DeserializationError(msg) => Self::DeserializationError(msg.clone()),
216 Self::SerializationError(msg) => Self::SerializationError(msg.clone()),
217 Self::SearchReplaceSyntaxError(msg) => Self::SearchReplaceSyntaxError(msg.clone()),
218 Self::SearchBlockNotFound(msg) => Self::SearchBlockNotFound(msg.clone()),
219 Self::SearchBlockAmbiguous { block_content, match_count, suggestions } => {
220 Self::SearchBlockAmbiguous {
221 block_content: block_content.clone(),
222 match_count: *match_count,
223 suggestions: suggestions.clone(),
224 }
225 }
226 Self::SearchReplaceSyntaxErrorDetailed {
227 message,
228 line_number,
229 block_type,
230 suggestions,
231 } => Self::SearchReplaceSyntaxErrorDetailed {
232 message: message.clone(),
233 line_number: *line_number,
234 block_type: block_type.clone(),
235 suggestions: suggestions.clone(),
236 },
237 Self::JsonParseError(msg) => Self::JsonParseError(msg.clone()),
238 Self::FileTooLarge { path, size, max_size } => {
239 Self::FileTooLarge { path: path.clone(), size: *size, max_size: *max_size }
240 }
241 Self::FileWriteError { path, message } => {
242 Self::FileWriteError { path: path.clone(), message: message.clone() }
243 }
244 Self::DataLoadingError(msg) => Self::DataLoadingError(msg.clone()),
245 Self::ParameterValidationError { field, message } => {
246 Self::ParameterValidationError { field: field.clone(), message: message.clone() }
247 }
248 Self::MissingParameterError { field, message } => {
249 Self::MissingParameterError { field: field.clone(), message: message.clone() }
250 }
251 Self::NullValueError { field } => Self::NullValueError { field: field.clone() },
252 Self::RecoverableSuggestionError { message, suggestion } => {
253 Self::RecoverableSuggestionError {
254 message: message.clone(),
255 suggestion: suggestion.clone(),
256 }
257 }
258 Self::ContextSaveError(msg) => Self::ContextSaveError(msg.clone()),
259 Self::CommandTimeout { command, timeout_seconds } => {
260 Self::CommandTimeout { command: command.clone(), timeout_seconds: *timeout_seconds }
261 }
262 Self::InteractiveCommandDetected { command } => {
263 Self::InteractiveCommandDetected { command: command.clone() }
264 }
265 Self::CommandAlreadyRunning { current_command, duration_seconds } => {
266 Self::CommandAlreadyRunning {
267 current_command: current_command.clone(),
268 duration_seconds: *duration_seconds,
269 }
270 }
271 Self::ProcessCleanupError { message } => {
272 Self::ProcessCleanupError { message: message.clone() }
273 }
274 Self::BufferOverflow { size, max_size } => {
275 Self::BufferOverflow { size: *size, max_size: *max_size }
276 }
277 Self::SessionRecoveryError { message } => {
278 Self::SessionRecoveryError { message: message.clone() }
279 }
280 Self::ResourceAllocationError { message } => {
281 Self::ResourceAllocationError { message: message.clone() }
282 }
283 Self::IoError(err) => Self::IoError(std::io::Error::new(err.kind(), err.to_string())),
284 Self::ConfigurationError(msg) => Self::ConfigurationError(msg.clone()),
285 Self::ParseError(msg) => Self::ParseError(msg.clone()),
286 Self::InvalidInput(msg) => Self::InvalidInput(msg.clone()),
287 Self::FileError(msg) => Self::FileError(msg.clone()),
288 Self::PathSecurityError { path, message } => {
289 Self::PathSecurityError { path: path.clone(), message: message.clone() }
290 }
291 }
292 }
293}