Skip to main content

nova_schema/
error.rs

1//! Error types for nova-schema.
2
3use thiserror::Error;
4
5/// Result type alias for schema operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during schema operations.
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Schema file not found.
12    #[error("schema not found: {name}")]
13    SchemaNotFound {
14        /// Name of the missing schema.
15        name: String,
16    },
17
18    /// Schema is invalid JSON Schema.
19    #[error("invalid schema definition: {message}")]
20    InvalidSchema {
21        /// Error message.
22        message: String,
23    },
24
25    /// Validation failed.
26    #[error("validation failed: {message}")]
27    ValidationFailed {
28        /// Error message.
29        message: String,
30        /// Path to the invalid field.
31        path: Option<String>,
32    },
33
34    /// Frontmatter parsing error.
35    #[error("frontmatter parse error: {message}")]
36    FrontmatterParse {
37        /// Error message.
38        message: String,
39        /// Line number if available.
40        line: Option<usize>,
41    },
42
43    /// YAML parsing error.
44    #[error("YAML error: {0}")]
45    Yaml(#[from] serde_yaml::Error),
46
47    /// TOML parsing error.
48    #[error("TOML error: {0}")]
49    Toml(#[from] toml::de::Error),
50
51    /// JSON error.
52    #[error("JSON error: {0}")]
53    Json(#[from] serde_json::Error),
54
55    /// I/O error.
56    #[error("I/O error: {0}")]
57    Io(#[from] std::io::Error),
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn error_display_schema_not_found() {
66        let err = Error::SchemaNotFound {
67            name: "ieee-article".to_string(),
68        };
69        assert_eq!(err.to_string(), "schema not found: ieee-article");
70    }
71
72    #[test]
73    fn error_display_validation_failed() {
74        let err = Error::ValidationFailed {
75            message: "missing required field".to_string(),
76            path: Some("/title".to_string()),
77        };
78        assert_eq!(err.to_string(), "validation failed: missing required field");
79    }
80}