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    /// A valid schema shape cannot be preserved by the requested transform.
19    #[error("unsupported json schema keyword {keyword} for {transform}")]
20    UnsupportedTransform {
21        /// Transform that cannot represent this schema.
22        transform: &'static str,
23        /// Unsupported schema keyword.
24        keyword: &'static str,
25    },
26    /// The JSON Schema itself is invalid.
27    #[error("invalid json schema: {message}")]
28    InvalidSchema {
29        /// Explanation.
30        message: String,
31    },
32}
33
34impl From<SchemaError> for ProviderError {
35    fn from(error: SchemaError) -> Self {
36        match error {
37            SchemaError::JsonParse(error) => Self::from(error),
38            SchemaError::TypeValidation(error) => Self::from(error),
39            error @ SchemaError::UnsupportedTransform { .. } => {
40                Self::from(InvalidArgumentError::new("schema", error.to_string()))
41            }
42            SchemaError::InvalidSchema { message } => {
43                Self::from(InvalidArgumentError::new("schema", message))
44            }
45        }
46    }
47}