1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! This module cotnains errors around entities not conforming to schemas
use super::TypeMismatchError;
use crate::ast::{EntityType, EntityUID};
use crate::extensions::ExtensionFunctionLookupError;
use miette::Diagnostic;
use smol_str::SmolStr;
use thiserror::Error;
/// Errors raised when entities do not conform to the schema
#[derive(Debug, Diagnostic, Error)]
#[non_exhaustive]
pub enum EntitySchemaConformanceError {
/// Encountered attribute that shouldn't exist on entities of this type
#[error(transparent)]
#[diagnostic(transparent)]
UnexpectedEntityAttr(UnexpectedEntityAttr),
/// Didn't encounter attribute that should exist
#[error(transparent)]
#[diagnostic(transparent)]
MissingRequiredEntityAttr(MissingRequiredEntityAttr),
/// The given attribute on the given entity had a different type than the
/// schema indicated
#[error(transparent)]
#[diagnostic(transparent)]
TypeMismatch(TypeMismatch),
/// Found an ancestor of a type that's not allowed for that entity
#[error(transparent)]
#[diagnostic(transparent)]
InvalidAncestorType(InvalidAncestorType),
/// Encountered an entity of a type which is not declared in the schema.
/// Note that this error is only used for non-Action entity types.
#[error(transparent)]
#[diagnostic(transparent)]
UnexpectedEntityType(#[from] UnexpectedEntityTypeError),
/// Encountered an action which was not declared in the schema
#[error(transparent)]
#[diagnostic(transparent)]
UndeclaredAction(UndeclaredAction),
/// Encountered an action whose definition doesn't precisely match the
/// schema's declaration of that action
#[error(transparent)]
#[diagnostic(transparent)]
ActionDeclarationMismatch(ActionDeclarationMismatch),
/// Error looking up an extension function. This error can occur when
/// checking entity conformance because that may require getting information
/// about any extension functions referenced in entity attribute values.
#[error(transparent)]
#[diagnostic(transparent)]
ExtensionFunctionLookup(ExtensionFunctionLookup),
}
impl EntitySchemaConformanceError {
pub(crate) fn unexpected_entity_attr(uid: EntityUID, attr: impl Into<SmolStr>) -> Self {
Self::UnexpectedEntityAttr(UnexpectedEntityAttr {
uid,
attr: attr.into(),
})
}
pub(crate) fn missing_entity_attr(uid: EntityUID, attr: impl Into<SmolStr>) -> Self {
Self::MissingRequiredEntityAttr(MissingRequiredEntityAttr {
uid,
attr: attr.into(),
})
}
pub(crate) fn type_mismatch(
uid: EntityUID,
attr: impl Into<SmolStr>,
err: TypeMismatchError,
) -> Self {
Self::TypeMismatch(TypeMismatch {
uid,
attr: attr.into(),
err,
})
}
pub(crate) fn invalid_ancestor_type(uid: EntityUID, ancestor_type: EntityType) -> Self {
Self::InvalidAncestorType(InvalidAncestorType {
uid,
ancestor_ty: Box::new(ancestor_type),
})
}
pub(crate) fn undeclared_action(uid: EntityUID) -> Self {
Self::UndeclaredAction(UndeclaredAction { uid })
}
pub(crate) fn action_declaration_mismatch(uid: EntityUID) -> Self {
Self::ActionDeclarationMismatch(ActionDeclarationMismatch { uid })
}
pub(crate) fn extension_function_lookup(
uid: EntityUID,
attr: impl Into<SmolStr>,
err: ExtensionFunctionLookupError,
) -> Self {
Self::ExtensionFunctionLookup(ExtensionFunctionLookup {
uid,
attr: attr.into(),
err,
})
}
}
/// Error looking up an extension function. This error can occur when
/// checking entity conformance because that may require getting information
/// about any extension functions referenced in entity attribute values.
#[derive(Debug, Error, Diagnostic)]
#[error("in attribute `{attr}` on `{uid}`, {err}")]
pub struct ExtensionFunctionLookup {
/// Entity where the error occurred
uid: EntityUID,
/// Name of the attribute where the error occurred
attr: SmolStr,
/// Underlying error
#[diagnostic(transparent)]
err: ExtensionFunctionLookupError,
}
/// Encountered an action whose definition doesn't precisely match the
/// schema's declaration of that action
#[derive(Debug, Error, Diagnostic)]
#[error("definition of action `{uid}` does not match its schema declaration")]
#[diagnostic(help(
"to use the schema's definition of `{uid}`, simply omit it from the entities input data"
))]
pub struct ActionDeclarationMismatch {
/// Action whose definition mismatched between entity data and schema
uid: EntityUID,
}
/// Encountered an action which was not declared in the schema
#[derive(Debug, Error, Diagnostic)]
#[error("found action entity `{uid}`, but it was not declared as an action in the schema")]
pub struct UndeclaredAction {
/// Action which was not declared in the schema
uid: EntityUID,
}
/// Found an ancestor of a type that's not allowed for that entity
#[derive(Debug, Error, Diagnostic)]
#[error(
"`{uid}` is not allowed to have an ancestor of type `{ancestor_ty}` according to the schema"
)]
pub struct InvalidAncestorType {
/// Entity that has an invalid ancestor type
uid: EntityUID,
/// Ancestor type which was invalid
ancestor_ty: Box<EntityType>, // boxed to avoid this variant being very large (and thus all EntitySchemaConformanceErrors being large)
}
/// Encountered attribute that shouldn't exist on entities of this type
#[derive(Debug, Error, Diagnostic)]
#[error("attribute `{attr}` on `{uid}` should not exist according to the schema")]
pub struct UnexpectedEntityAttr {
uid: EntityUID,
attr: SmolStr,
}
/// Didn't encounter attribute that should exist
#[derive(Debug, Error, Diagnostic)]
#[error("expected entity `{uid}` to have attribute `{attr}`, but it does not")]
pub struct MissingRequiredEntityAttr {
uid: EntityUID,
attr: SmolStr,
}
#[derive(Debug, Error, Diagnostic)]
#[error("in attribute `{attr}` on `{uid}`, {err}")]
/// The given attribute on the given entity had a different type than the
/// schema indicated
pub struct TypeMismatch {
uid: EntityUID,
attr: SmolStr,
#[diagnostic(transparent)]
err: TypeMismatchError,
}
/// Encountered an entity of a type which is not declared in the schema.
/// Note that this error is only used for non-Action entity types.
#[derive(Debug, Error)]
#[error("entity `{uid}` has type `{}` which is not declared in the schema", .uid.entity_type())]
pub struct UnexpectedEntityTypeError {
/// Entity that had the unexpected type
pub uid: EntityUID,
/// Suggested similar entity types that actually are declared in the schema (if any)
pub suggested_types: Vec<EntityType>,
}
impl Diagnostic for UnexpectedEntityTypeError {
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
match self.suggested_types.as_slice() {
[] => None,
[ty] => Some(Box::new(format!("did you mean `{ty}`?"))),
tys => Some(Box::new(format!(
"did you mean one of {:?}?",
tys.iter().map(ToString::to_string).collect::<Vec<String>>()
))),
}
}
}