Skip to main content

openapi_to_rust/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum GeneratorError {
5    #[error("Failed to parse OpenAPI spec: {0}")]
6    ParseError(#[from] serde_json::Error),
7
8    /// Document-level parse failure located to the node that failed. Serde's
9    /// own message for an untagged enum ("data did not match any variant of
10    /// untagged enum Schema") names no field, so a large spec would otherwise
11    /// have to be bisected by hand to find the offending node.
12    #[error("Failed to parse OpenAPI spec at {pointer}: {message}")]
13    ParseErrorAt { pointer: String, message: String },
14
15    #[error("Unresolved schema reference: {0}")]
16    UnresolvedReference(String),
17
18    #[error("Circular dependency detected: {0}")]
19    CircularDependency(String),
20
21    #[error("Invalid discriminator pattern in schema: {0}")]
22    InvalidDiscriminator(String),
23
24    #[error("Code generation failed: {0}")]
25    CodeGenError(String),
26
27    #[error("IO error: {0}")]
28    IoError(#[from] std::io::Error),
29
30    #[error("Invalid schema: {0}")]
31    InvalidSchema(String),
32
33    #[error("Validation error: {0}")]
34    ValidationError(String),
35
36    #[error("File error: {message}")]
37    FileError { message: String },
38}
39
40impl GeneratorError {
41    pub fn with_context(self, schema_name: &str) -> Self {
42        match self {
43            Self::CodeGenError(msg) => {
44                Self::CodeGenError(format!("{msg} (in schema: {schema_name})"))
45            }
46            Self::InvalidSchema(msg) => {
47                Self::InvalidSchema(format!("{msg} (in schema: {schema_name})"))
48            }
49            other => other,
50        }
51    }
52}