Skip to main content

ferrin_schema/
error.rs

1//! Schema-layer error type.
2
3use ferrin_spec::error::InvalidArgumentError;
4use ferrin_spec::error::JsonParseError;
5use ferrin_spec::error::ProviderError;
6use ferrin_spec::error::TypeValidationError;
7
8/// Errors produced while parsing or validating JSON against a schema.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum SchemaError {
12    /// The text is not valid JSON.
13    #[error(transparent)]
14    JsonParse(#[from] JsonParseError),
15    /// The value does not match the schema or type.
16    #[error(transparent)]
17    TypeValidation(#[from] TypeValidationError),
18    /// The JSON Schema itself is invalid.
19    #[error("invalid json schema: {message}")]
20    InvalidSchema {
21        /// Explanation.
22        message: String,
23    },
24}
25
26impl From<SchemaError> for ProviderError {
27    fn from(error: SchemaError) -> Self {
28        match error {
29            SchemaError::JsonParse(error) => Self::from(error),
30            SchemaError::TypeValidation(error) => Self::from(error),
31            SchemaError::InvalidSchema { message } => {
32                Self::from(InvalidArgumentError::new("schema", message))
33            }
34        }
35    }
36}