openapi_nexus_typescript/
errors.rs

1//! Error types for TypeScript code generation
2
3use snafu::Snafu;
4
5/// Error type for TypeScript code generation
6#[derive(Debug, Snafu)]
7#[snafu(visibility(pub))]
8pub enum GeneratorError {
9    /// Template rendering error
10    #[snafu(display("Failed to render template '{}': {}", template_path, source))]
11    TemplateRender {
12        template_path: String,
13        source: minijinja::Error,
14    },
15
16    /// Template not found error
17    #[snafu(display("Template '{}' not found: {}", template_path, source))]
18    TemplateNotFound {
19        template_path: String,
20        source: minijinja::Error,
21    },
22
23    /// API class generation error
24    #[snafu(display("Failed to generate API class '{}': {}", class_name, source))]
25    ApiClassGeneration {
26        class_name: String,
27        source: Box<dyn std::error::Error + Send + Sync>,
28    },
29
30    /// API class generation error for tag
31    #[snafu(display("Failed to generate API class for tag '{}': {}", tag, source))]
32    ApiClassGenerationForTag {
33        tag: String,
34        source: Box<dyn std::error::Error + Send + Sync>,
35    },
36
37    /// Model interface generation error
38    #[snafu(display("Failed to generate interface model '{}': {}", model_name, source))]
39    ModelInterfaceGeneration {
40        model_name: String,
41        source: Box<dyn std::error::Error + Send + Sync>,
42    },
43
44    /// Model type alias generation error
45    #[snafu(display("Failed to generate type alias model '{}': {}", model_name, source))]
46    ModelTypeAliasGeneration {
47        model_name: String,
48        source: Box<dyn std::error::Error + Send + Sync>,
49    },
50
51    /// Model enum generation error
52    #[snafu(display("Failed to generate enum model '{}': {}", model_name, source))]
53    ModelEnumGeneration {
54        model_name: String,
55        source: Box<dyn std::error::Error + Send + Sync>,
56    },
57
58    /// Runtime template generation error
59    #[snafu(display("Failed to render runtime template: {}", source))]
60    RuntimeTemplate {
61        source: Box<dyn std::error::Error + Send + Sync>,
62    },
63
64    /// Index file generation error
65    #[snafu(display("Failed to render index file '{}': {}", file_path, source))]
66    IndexFileGeneration {
67        file_path: String,
68        source: Box<dyn std::error::Error + Send + Sync>,
69    },
70
71    /// Unsupported HTTP method error
72    #[snafu(display(
73        "Unsupported HTTP method: {:?}. Only GET, POST, PUT, PATCH, and DELETE are supported.",
74        method
75    ))]
76    UnsupportedHttpMethod { method: http::Method },
77
78    /// Parameter extraction error
79    #[snafu(display("Failed to extract parameters: {}", source))]
80    ParameterExtraction {
81        source: Box<dyn std::error::Error + Send + Sync>,
82    },
83
84    /// Return type generation error
85    #[snafu(display("Failed to generate return type: {}", source))]
86    ReturnTypeGeneration {
87        source: Box<dyn std::error::Error + Send + Sync>,
88    },
89
90    /// File I/O error
91    #[snafu(display("File I/O error: {}", source))]
92    Io { source: std::io::Error },
93
94    /// Config parsing error
95    #[snafu(display("Failed to parse config: {}", source))]
96    ConfigParse { source: toml::de::Error },
97
98    /// Generic error for cases that don't fit other categories
99    #[snafu(display("Generator error: {}", message))]
100    Generic { message: String },
101}
102
103impl From<minijinja::Error> for GeneratorError {
104    fn from(err: minijinja::Error) -> Self {
105        GeneratorError::Generic {
106            message: err.to_string(),
107        }
108    }
109}
110
111impl From<std::io::Error> for GeneratorError {
112    fn from(err: std::io::Error) -> Self {
113        GeneratorError::Io { source: err }
114    }
115}
116
117impl From<toml::de::Error> for GeneratorError {
118    fn from(err: toml::de::Error) -> Self {
119        GeneratorError::ConfigParse { source: err }
120    }
121}
122
123impl From<Box<dyn std::error::Error + Send + Sync>> for GeneratorError {
124    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
125        GeneratorError::Generic {
126            message: err.to_string(),
127        }
128    }
129}