bevy_agent/
error.rs

1//! Error types and utilities for Bevy AI
2
3use thiserror::Error;
4
5/// Result type alias for Bevy AI operations
6pub type Result<T> = std::result::Result<T, BevyAIError>;
7
8/// Main error type for Bevy AI operations
9#[derive(Error, Debug)]
10pub enum BevyAIError {
11    /// Configuration-related errors
12    #[error("Configuration error: {0}")]
13    Config(#[from] config::ConfigError),
14    
15    /// HTTP request errors
16    #[error("HTTP request failed: {0}")]
17    Http(#[from] reqwest::Error),
18    
19    /// JSON serialization/deserialization errors  
20    #[error("JSON serialization/deserialization error: {0}")]
21    Json(#[from] serde_json::Error),
22    
23    /// Input/output errors
24    #[error("IO error: {0}")]
25    Io(#[from] std::io::Error),
26    
27    /// File system traversal errors
28    #[error("File traversal error: {0}")]
29    WalkDir(#[from] walkdir::Error),
30    
31    /// Template rendering errors
32    #[error("Template rendering error: {0}")]
33    Template(#[from] handlebars::RenderError),
34    
35    /// Template creation errors
36    #[error("Template creation error: {0}")]
37    TemplateCreation(#[from] handlebars::TemplateError),
38    
39    /// Code parsing errors
40    #[error("Code parsing error: {0}")]
41    CodeParsing(String),
42    
43    /// AI API communication errors
44    #[error("AI API error: {message}")]
45    AIApi { 
46        /// The error message from the AI API
47        message: String 
48    },
49    
50    /// Missing API key errors
51    #[error("API key not configured for {provider}")]
52    MissingApiKey { 
53        /// The AI provider that requires an API key
54        provider: String 
55    },
56    
57    /// Unsupported model errors
58    #[error("Unsupported AI model: {model}")]
59    UnsupportedModel { 
60        /// The unsupported model name
61        model: String 
62    },
63    
64    /// Project not found errors
65    #[error("Project not found at path: {path}")]
66    ProjectNotFound { 
67        /// The path where the project was expected
68        path: String 
69    },
70    
71    /// Invalid project structure errors
72    #[error("Invalid project structure: {reason}")]
73    InvalidProject { 
74        /// The reason why the project is invalid
75        reason: String 
76    },
77    
78    /// Feature generation errors
79    #[error("Feature generation failed: {reason}")]
80    FeatureGeneration { 
81        /// The reason why feature generation failed
82        reason: String 
83    },
84    
85    /// Code optimization errors
86    #[error("Code optimization failed: {reason}")]
87    CodeOptimization { 
88        /// The reason why code optimization failed
89        reason: String 
90    },
91    
92    /// File operation errors
93    #[error("File operation failed: {operation} on {path}")]
94    FileOperation { 
95        /// The operation that failed
96        operation: String, 
97        /// The path where the operation failed
98        path: String 
99    },
100    
101    /// Template not found errors
102    #[error("Template not found: {name}")]
103    TemplateNotFound { 
104        /// The name of the template that wasn't found
105        name: String 
106    },
107    
108    /// Dependency resolution errors
109    #[error("Dependency resolution failed: {dependency}")]
110    DependencyResolution { 
111        /// The dependency that couldn't be resolved
112        dependency: String 
113    },
114    
115    /// Build system errors
116    #[error("Build system error: {message}")]
117    BuildSystem { 
118        /// The build system error message
119        message: String 
120    },
121    
122    /// Validation errors
123    #[error("Validation error: {message}")]
124    Validation { 
125        /// The validation error message
126        message: String 
127    },
128}
129
130impl BevyAIError {
131    /// Create a new AI API error
132    pub fn ai_api<S: Into<String>>(message: S) -> Self {
133        Self::AIApi {
134            message: message.into(),
135        }
136    }
137    
138    /// Create a new missing API key error
139    pub fn missing_api_key<S: Into<String>>(provider: S) -> Self {
140        Self::MissingApiKey {
141            provider: provider.into(),
142        }
143    }
144    
145    /// Create a new unsupported model error
146    pub fn unsupported_model<S: Into<String>>(model: S) -> Self {
147        Self::UnsupportedModel {
148            model: model.into(),
149        }
150    }
151    
152    /// Create a new project not found error
153    pub fn project_not_found<S: Into<String>>(path: S) -> Self {
154        Self::ProjectNotFound {
155            path: path.into(),
156        }
157    }
158    
159    /// Create a new invalid project error
160    pub fn invalid_project<S: Into<String>>(reason: S) -> Self {
161        Self::InvalidProject {
162            reason: reason.into(),
163        }
164    }
165    
166    /// Create a new feature generation error
167    pub fn feature_generation<S: Into<String>>(reason: S) -> Self {
168        Self::FeatureGeneration {
169            reason: reason.into(),
170        }
171    }
172    
173    /// Create a new code optimization error
174    pub fn code_optimization<S: Into<String>>(reason: S) -> Self {
175        Self::CodeOptimization {
176            reason: reason.into(),
177        }
178    }
179    
180    /// Create a new file operation error
181    pub fn file_operation<S: Into<String>>(operation: S, path: S) -> Self {
182        Self::FileOperation {
183            operation: operation.into(),
184            path: path.into(),
185        }
186    }
187    
188    /// Create a new template not found error
189    pub fn template_not_found<S: Into<String>>(name: S) -> Self {
190        Self::TemplateNotFound {
191            name: name.into(),
192        }
193    }
194    
195    /// Create a new dependency resolution error
196    pub fn dependency_resolution<S: Into<String>>(dependency: S) -> Self {
197        Self::DependencyResolution {
198            dependency: dependency.into(),
199        }
200    }
201    
202    /// Create a new build system error
203    pub fn build_system<S: Into<String>>(message: S) -> Self {
204        Self::BuildSystem {
205            message: message.into(),
206        }
207    }
208    
209    /// Create a new validation error
210    pub fn validation<S: Into<String>>(message: S) -> Self {
211        Self::Validation {
212            message: message.into(),
213        }
214    }
215}