1use thiserror::Error;
2
3#[derive(Debug, Error)]
27pub enum VikaError {
28 #[error("Schema error: {0}")]
29 Schema(#[from] SchemaError),
30
31 #[error("Config error: {0}")]
32 Config(#[from] ConfigError),
33
34 #[error("Network error: {0}")]
35 Network(#[from] NetworkError),
36
37 #[error("File system error: {0}")]
38 FileSystem(#[from] FileSystemError),
39
40 #[error("Generation error: {0}")]
41 Generation(#[from] GenerationError),
42
43 #[error("Validation error: {0}")]
44 Validation(#[from] ValidationError),
45}
46
47#[derive(Debug, Error)]
48pub enum SchemaError {
49 #[error("Schema not found: {name}")]
50 NotFound { name: String },
51
52 #[error("Invalid schema reference: {ref_path}")]
53 InvalidReference { ref_path: String },
54
55 #[error("Circular reference detected: {cycle:?}")]
56 CircularReference { cycle: Vec<String> },
57
58 #[error("Unsupported schema type: {schema_type}")]
59 UnsupportedType { schema_type: String },
60
61 #[error("Parameter not found: {name}")]
62 ParameterNotFound { name: String },
63
64 #[error("Request body not found: {name}")]
65 RequestBodyNotFound { name: String },
66
67 #[error("Response not found: {name}")]
68 ResponseNotFound { name: String },
69
70 #[error("Unsupported reference path: {ref_path}")]
71 UnsupportedReferencePath { ref_path: String },
72}
73
74#[derive(Debug, Error)]
75pub enum ConfigError {
76 #[error("Config file not found: {path}")]
77 NotFound { path: String },
78
79 #[error("Invalid config: {message}")]
80 Invalid { message: String },
81
82 #[error("Failed to read config: {0}")]
83 ReadError(#[from] std::io::Error),
84
85 #[error("Failed to parse config: {0}")]
86 ParseError(#[from] serde_json::Error),
87
88 #[error("Required field missing: {field}")]
89 MissingField { field: String },
90
91 #[error("Invalid output directory: {path}")]
92 InvalidOutputDirectory { path: String },
93}
94
95#[derive(Debug, Error)]
96pub enum NetworkError {
97 #[error("Failed to fetch spec from {url}: {source}")]
98 FetchFailed {
99 url: String,
100 #[source]
101 source: reqwest::Error,
102 },
103
104 #[error("Failed to read response from {url}: {source}")]
105 ReadResponseFailed {
106 url: String,
107 #[source]
108 source: reqwest::Error,
109 },
110
111 #[error("Invalid URL: {url}")]
112 InvalidUrl { url: String },
113}
114
115#[derive(Debug, Error)]
116pub enum FileSystemError {
117 #[error("Failed to create directory: {path}")]
118 CreateDirectoryFailed {
119 path: String,
120 #[source]
121 source: std::io::Error,
122 },
123
124 #[error("Failed to read file: {path}")]
125 ReadFileFailed {
126 path: String,
127 #[source]
128 source: std::io::Error,
129 },
130
131 #[error("Failed to write file: {path}")]
132 WriteFileFailed {
133 path: String,
134 #[source]
135 source: std::io::Error,
136 },
137
138 #[error("File not found: {path}")]
139 FileNotFound { path: String },
140
141 #[error("Directory not found: {path}")]
142 DirectoryNotFound { path: String },
143
144 #[error("File was modified by user: {path}\n This file was previously generated by vika-cli but has been modified.\n Use --force to overwrite, or --backup to create a backup first.")]
145 FileModifiedByUser { path: String },
146}
147
148#[derive(Debug, Error)]
149pub enum GenerationError {
150 #[error("No modules available after filtering")]
151 NoModulesAvailable,
152
153 #[error("No modules selected")]
154 NoModulesSelected,
155
156 #[error("Spec path or URL is required. Use --spec <path-or-url>")]
157 SpecPathRequired,
158
159 #[error("Failed to generate TypeScript types: {0}")]
160 TypeScriptGenerationFailed(String),
161
162 #[error("Failed to generate Zod schemas: {0}")]
163 ZodGenerationFailed(String),
164
165 #[error("Failed to generate API client: {0}")]
166 ApiClientGenerationFailed(String),
167
168 #[error("Invalid operation: {message}")]
169 InvalidOperation { message: String },
170
171 #[error("Template error: {0}")]
172 Template(#[from] TemplateError),
173}
174
175#[derive(Debug, Error)]
176pub enum ValidationError {
177 #[error("Invalid TypeScript identifier: {name}")]
178 InvalidTypeScriptIdentifier { name: String },
179
180 #[error("Invalid module name: {name}")]
181 InvalidModuleName { name: String },
182
183 #[error("Validation failed: {message}")]
184 Failed { message: String },
185}
186
187#[derive(Debug, Error)]
188pub enum TemplateError {
189 #[error("Template not found: {name}")]
190 NotFound { name: String },
191
192 #[error("Failed to render template {name}: {message}")]
193 RenderFailed { name: String, message: String },
194
195 #[error("Invalid template syntax in {name}: {message}")]
196 InvalidSyntax { name: String, message: String },
197
198 #[error("Missing required context field {field} in template {name}")]
199 MissingContextField { name: String, field: String },
200
201 #[error("Failed to load template {name}: {source}")]
202 LoadFailed {
203 name: String,
204 #[source]
205 source: FileSystemError,
206 },
207}
208
209pub type Result<T> = std::result::Result<T, VikaError>;
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn test_schema_error_display() {
217 let error = SchemaError::NotFound {
218 name: "User".to_string(),
219 };
220 let error_msg = error.to_string();
221 assert!(error_msg.contains("User"));
222 assert!(error_msg.contains("not found"));
223 }
224
225 #[test]
226 fn test_config_error_display() {
227 let error = ConfigError::Invalid {
228 message: "Test error".to_string(),
229 };
230 let error_msg = error.to_string();
231 assert!(error_msg.contains("Test error"));
232 }
233
234 #[test]
235 fn test_network_error_display() {
236 let error = NetworkError::InvalidUrl {
237 url: "invalid-url".to_string(),
238 };
239 let error_msg = error.to_string();
240 assert!(error_msg.contains("invalid-url"));
241 }
242
243 #[test]
244 fn test_file_system_error_display() {
245 let error = FileSystemError::FileNotFound {
246 path: "/test/path".to_string(),
247 };
248 let error_msg = error.to_string();
249 assert!(error_msg.contains("/test/path"));
250 }
251
252 #[test]
253 fn test_generation_error_display() {
254 let error = GenerationError::NoModulesAvailable;
255 let error_msg = error.to_string();
256 assert!(error_msg.contains("No modules available"));
257 }
258
259 #[test]
260 fn test_error_conversion() {
261 let schema_error = SchemaError::NotFound {
262 name: "Test".to_string(),
263 };
264 let vika_error: VikaError = schema_error.into();
265 let error_msg = vika_error.to_string();
266 assert!(error_msg.contains("Schema error"));
267 }
268
269 #[test]
270 fn test_error_context_preservation() {
271 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
272 let config_error = ConfigError::ReadError(io_error);
273 let vika_error: VikaError = config_error.into();
274 let error_msg = vika_error.to_string();
275 assert!(error_msg.contains("Config error"));
276 }
277}