Skip to main content

cedar_policy_core/entities/
err.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::EntityUID;
18use crate::transitive_closure;
19use miette::Diagnostic;
20use thiserror::Error;
21
22/// Errors in serializing, deserializing, and processing of Entities
23#[derive(Debug, Diagnostic, Error)]
24pub enum EntitiesError {
25    /// Error occurring in serialization of entities
26    #[error("error during entity serialization: {0}")]
27    #[diagnostic(transparent)]
28    Serialization(#[from] crate::entities::json::err::JsonSerializationError),
29    /// Error occurring in deserialization of entities
30    #[error("error during entity deserialization")]
31    #[diagnostic(transparent)]
32    Deserialization(#[from] crate::entities::json::err::JsonDeserializationError),
33    /// Error constructing the Entities collection due to encountering two different entities
34    /// with the same Entity UID
35    #[error(transparent)]
36    #[diagnostic(transparent)]
37    Duplicate(Duplicate),
38    /// Errors occurring while computing or enforcing transitive closure on the
39    /// entity hierarchy.
40    #[error("transitive closure computation/enforcement error")]
41    #[diagnostic(transparent)]
42    TransitiveClosureError(#[from] TransitiveClosureError),
43    /// Error because an entity doesn't conform to the schema
44    #[error("entity does not conform to the schema")]
45    #[diagnostic(transparent)]
46    InvalidEntity(#[from] crate::entities::conformance::err::EntitySchemaConformanceError),
47    /// Error because an entity is not well-formed
48    #[error(transparent)]
49    #[diagnostic(transparent)]
50    InvalidEntityStructure(#[from] InvalidEntityStructureError),
51}
52
53impl EntitiesError {
54    pub(crate) fn duplicate(euid: EntityUID) -> Self {
55        Self::Duplicate(Duplicate { euid })
56    }
57}
58
59impl From<transitive_closure::TcError<EntityUID>> for EntitiesError {
60    fn from(value: transitive_closure::TcError<EntityUID>) -> Self {
61        let tc: TransitiveClosureError = value.into();
62        tc.into()
63    }
64}
65
66#[derive(Debug, Error, Diagnostic)]
67#[error(transparent)]
68#[diagnostic(transparent)]
69/// Errors occurring while computing or enforcing transitive closure on the
70/// entity hierarchy.
71pub struct TransitiveClosureError {
72    err: Box<transitive_closure::TcError<EntityUID>>,
73}
74
75#[cfg(test)]
76impl TransitiveClosureError {
77    pub(crate) fn inner(&self) -> &transitive_closure::TcError<EntityUID> {
78        self.err.as_ref()
79    }
80}
81
82impl From<transitive_closure::TcError<EntityUID>> for TransitiveClosureError {
83    fn from(v: transitive_closure::TcError<EntityUID>) -> Self {
84        Self { err: Box::new(v) }
85    }
86}
87
88#[derive(Debug, PartialEq, Eq, Error, Diagnostic)]
89#[error("duplicate entity entry `{}`", .euid)]
90/// Error type for entity sets containing duplicate entity uids
91pub struct Duplicate {
92    /// The [`EntityUID`] that was duplicated
93    pub(crate) euid: EntityUID,
94}
95
96#[cfg(test)]
97impl Duplicate {
98    pub(crate) fn euid(&self) -> &EntityUID {
99        &self.euid
100    }
101}
102
103/// Type alias for convenience
104pub type Result<T> = std::result::Result<T, EntitiesError>;
105
106/// Error type when an entity is not well formed used in entity validation.
107#[derive(Debug, Error, Diagnostic)]
108pub enum InvalidEntityStructureError {
109    /// Entity has itself as an ancestor
110    #[error("entity `{uid}` has itself as an ancestor")]
111    SelfAncestor {
112        /// The entity UID
113        uid: EntityUID,
114    },
115    /// Parents and indirect ancestors overlap
116    #[error("entity `{uid}` has `{ancestor}` in both parents and indirect ancestors")]
117    DuplicateAncestor {
118        /// The entity UID
119        uid: EntityUID,
120        /// The ancestor that appears in both sets
121        ancestor: EntityUID,
122    },
123    /// Action entity has a non-action parent
124    #[error("action `{uid}` has a non-action parent `{parent}`")]
125    ActionParentIsNotAction {
126        /// The action entity UID
127        uid: EntityUID,
128        /// The non-action parent
129        parent: EntityUID,
130    },
131}