Skip to main content

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    /// Conflicting schema names error
103    ///
104    /// This error occurs when multiple schema names map to the same PascalCase name,
105    /// which would cause TypeScript compilation errors.
106    #[snafu(display("{message}"))]
107    ConflictingSchemaNames { message: String },
108}
109
110impl From<minijinja::Error> for GeneratorError {
111    fn from(err: minijinja::Error) -> Self {
112        GeneratorError::Generic {
113            message: err.to_string(),
114        }
115    }
116}
117
118impl From<std::io::Error> for GeneratorError {
119    fn from(err: std::io::Error) -> Self {
120        GeneratorError::Io { source: err }
121    }
122}
123
124impl From<toml::de::Error> for GeneratorError {
125    fn from(err: toml::de::Error) -> Self {
126        GeneratorError::ConfigParse { source: err }
127    }
128}
129
130impl From<Box<dyn std::error::Error + Send + Sync>> for GeneratorError {
131    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
132        GeneratorError::Generic {
133            message: err.to_string(),
134        }
135    }
136}