Skip to main content

stix_model/
error.rs

1//! Error type for the object model.
2
3use thiserror::Error;
4
5/// Errors produced while importing or interpreting STIX objects.
6#[derive(Debug, Error)]
7pub enum ModelError {
8    /// The JSON could not be parsed.
9    #[error("invalid JSON: {0}")]
10    Json(#[from] serde_json::Error),
11
12    /// A required property was missing or had the wrong type.
13    #[error("invalid STIX object: {0}")]
14    InvalidObject(String),
15
16    /// The input was not a STIX bundle.
17    #[error("not a STIX bundle: {0}")]
18    NotABundle(String),
19}
20
21/// Convenience alias for results in this crate.
22pub type Result<T> = std::result::Result<T, ModelError>;
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn invalid_object_displays_message() {
30        let e = ModelError::InvalidObject("missing id".to_string());
31        assert!(format!("{e}").contains("missing id"));
32    }
33
34    #[test]
35    fn json_error_converts() {
36        let json_err = serde_json::from_str::<serde_json::Value>("{bad").unwrap_err();
37        let e: ModelError = json_err.into();
38        assert!(matches!(e, ModelError::Json(_)));
39    }
40}