Skip to main content

unistructgen_openapi_parser/
error.rs

1//! Error types for OpenAPI parser
2
3use thiserror::Error;
4
5/// Result type for OpenAPI parser operations
6pub type Result<T> = std::result::Result<T, OpenApiError>;
7
8/// Errors that can occur during OpenAPI parsing
9#[derive(Error, Debug)]
10pub enum OpenApiError {
11    /// Failed to parse YAML
12    #[error("Failed to parse YAML: {0}")]
13    YamlParse(#[from] serde_yaml::Error),
14
15    /// Failed to parse JSON
16    #[error("Failed to parse JSON: {0}")]
17    JsonParse(#[from] serde_json::Error),
18
19    /// Invalid OpenAPI specification
20    #[error("Invalid OpenAPI specification: {0}")]
21    InvalidSpec(String),
22
23    /// Missing required field
24    #[error("Missing required field: {0}")]
25    MissingField(String),
26
27    /// Unsupported schema type
28    #[error("Unsupported schema type: {0}")]
29    UnsupportedType(String),
30
31    /// Reference resolution failed
32    #[error("Failed to resolve reference '{reference}': {reason}")]
33    ReferenceResolution {
34        /// The reference that couldn't be resolved
35        reference: String,
36        /// Why it failed
37        reason: String,
38    },
39
40    /// Circular reference detected
41    #[error("Circular reference detected: {0}")]
42    CircularReference(String),
43
44    /// Invalid schema composition
45    #[error("Invalid schema composition (allOf/oneOf/anyOf): {0}")]
46    InvalidComposition(String),
47
48    /// HTTP fetch error
49    #[cfg(feature = "fetch")]
50    #[error("Failed to fetch OpenAPI spec from URL: {0}")]
51    FetchError(String),
52
53    /// IO error
54    #[error("IO error: {0}")]
55    Io(#[from] std::io::Error),
56
57    /// Generic error
58    #[error("{0}")]
59    Other(String),
60}
61
62impl OpenApiError {
63    /// Create a new invalid spec error
64    pub fn invalid_spec(msg: impl Into<String>) -> Self {
65        Self::InvalidSpec(msg.into())
66    }
67
68    /// Create a new missing field error
69    pub fn missing_field(field: impl Into<String>) -> Self {
70        Self::MissingField(field.into())
71    }
72
73    /// Create a new unsupported type error
74    pub fn unsupported_type(type_name: impl Into<String>) -> Self {
75        Self::UnsupportedType(type_name.into())
76    }
77
78    /// Create a new reference resolution error
79    pub fn reference_resolution(reference: impl Into<String>, reason: impl Into<String>) -> Self {
80        Self::ReferenceResolution {
81            reference: reference.into(),
82            reason: reason.into(),
83        }
84    }
85
86    /// Create a new circular reference error
87    pub fn circular_reference(path: impl Into<String>) -> Self {
88        Self::CircularReference(path.into())
89    }
90
91    /// Create a new invalid composition error
92    pub fn invalid_composition(msg: impl Into<String>) -> Self {
93        Self::InvalidComposition(msg.into())
94    }
95}