use thiserror::Error;
use super::PropertyType;
#[derive(Debug, Error, Clone)]
pub enum SchemaError {
#[error("unknown vertex label: {label}")]
UnknownVertexLabel {
label: String,
},
#[error("unknown edge label: {label}")]
UnknownEdgeLabel {
label: String,
},
#[error("missing required property '{property}' on {element_type} '{label}'")]
MissingRequired {
element_type: &'static str,
label: String,
property: String,
},
#[error("type mismatch for property '{property}': expected {expected}, got {actual}")]
TypeMismatch {
property: String,
expected: PropertyType,
actual: String,
},
#[error("unexpected property '{property}' on {element_type} '{label}'")]
UnexpectedProperty {
element_type: &'static str,
label: String,
property: String,
},
#[error("invalid edge endpoint: '{edge_label}' cannot connect from '{from_label}' (allowed: {allowed:?})")]
InvalidSourceLabel {
edge_label: String,
from_label: String,
allowed: Vec<String>,
},
#[error("invalid edge endpoint: '{edge_label}' cannot connect to '{to_label}' (allowed: {allowed:?})")]
InvalidTargetLabel {
edge_label: String,
to_label: String,
allowed: Vec<String>,
},
#[error("null value for required property '{property}' on {element_type} '{label}'")]
NullRequired {
element_type: &'static str,
label: String,
property: String,
},
#[error("type '{name}' already exists")]
TypeAlreadyExists {
name: String,
},
#[error("type '{name}' not found")]
TypeNotFound {
name: String,
},
#[error("property '{property}' already exists on type '{type_name}'")]
PropertyAlreadyExists {
type_name: String,
property: String,
},
#[error("property '{property}' not found on type '{type_name}'")]
PropertyNotFound {
type_name: String,
property: String,
},
#[error("edge type must specify FROM and TO endpoint constraints")]
MissingEndpointConstraints,
#[error("index DDL must be executed via graph.create_index() or graph.drop_index()")]
IndexDdlNotSupported,
}
pub type SchemaResult<T> = Result<T, SchemaError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_messages_are_descriptive() {
let err = SchemaError::MissingRequired {
element_type: "vertex",
label: "Person".to_string(),
property: "name".to_string(),
};
assert!(err.to_string().contains("name"));
assert!(err.to_string().contains("Person"));
assert!(err.to_string().contains("vertex"));
let err = SchemaError::TypeMismatch {
property: "age".to_string(),
expected: PropertyType::Int,
actual: "STRING".to_string(),
};
assert!(err.to_string().contains("age"));
assert!(err.to_string().contains("INT"));
assert!(err.to_string().contains("STRING"));
}
#[test]
fn error_is_clone() {
let err = SchemaError::UnknownVertexLabel {
label: "Test".to_string(),
};
let cloned = err.clone();
assert_eq!(err.to_string(), cloned.to_string());
}
}