cedar_policy_core/est/
err.rs1use std::sync::Arc;
18
19use crate::ast;
20use crate::ast::PolicySetError;
21use crate::entities::JsonDeserializationError;
22use crate::parser::err::{parse_errors, ParseErrors};
23use crate::parser::{join_with_conjunction, unescape};
24use miette::Diagnostic;
25use nonempty::NonEmpty;
26use smol_str::SmolStr;
27use thiserror::Error;
28
29#[derive(Debug, Diagnostic, Error)]
31pub enum FromJsonError {
32 #[error(transparent)]
34 #[diagnostic(transparent)]
35 JsonDeserializationError(#[from] JsonDeserializationError),
36 #[error(transparent)]
38 #[diagnostic(transparent)]
39 TemplateToPolicy(#[from] parse_errors::ExpectedStaticPolicy),
40 #[error(transparent)]
42 #[diagnostic(transparent)]
43 PolicyToTemplate(#[from] parse_errors::ExpectedTemplate),
44 #[error("invalid slot name or slot used in wrong position")]
47 #[diagnostic(help(
48 "principal slots must be named `?principal` and resource slots must be named `?resource`"
49 ))]
50 InvalidSlotName,
51 #[error("slots are not allowed for actions")]
53 ActionSlot,
54 #[error("found template slot {slot} in a `{clausetype}` clause")]
56 #[diagnostic(help("slots are currently unsupported in `{clausetype}` clauses"))]
57 SlotsInConditionClause {
58 slot: ast::SlotId,
60 clausetype: &'static str,
62 },
63 #[error("missing operator, found empty object")]
65 MissingOperator,
66 #[error("found multiple operators where one was expected: {ops:?}")]
68 MultipleOperators {
69 ops: Vec<SmolStr>,
71 },
72 #[error(
74 "multiplication must be by a constant int: neither `{arg1}` nor `{arg2}` is a constant"
75 )]
76 MultiplicationByNonConstant {
77 arg1: ast::Expr,
79 arg2: ast::Expr,
81 },
82 #[error("{}", match .0.first() { Some(err) => format!("{err}"), None => "invalid escape".into() })]
85 UnescapeError(#[related] Vec<unescape::UnescapeError>),
86 #[error("invalid entity type: {0}")]
88 #[diagnostic(transparent)]
89 InvalidEntityType(ParseErrors),
90 #[error("Error creating policy set: {0}")]
92 #[diagnostic(transparent)]
93 PolicySet(#[from] PolicySetError),
94 #[error("Error linking policy set: {0}")]
96 #[diagnostic(transparent)]
97 Linking(#[from] ast::LinkingError),
98 #[error("Invalid extension function name: `{0}`")]
100 UnknownExtFunc(ast::Name),
101 #[error(transparent)]
103 #[diagnostic(transparent)]
104 InvalidActionType(#[from] InvalidActionType),
105}
106
107#[derive(Debug, Diagnostic, Error)]
109#[diagnostic(help("action entities must have type `Action`, optionally in a namespace"))]
110pub struct InvalidActionType {
111 pub(crate) euids: NonEmpty<Arc<crate::ast::EntityUID>>,
112}
113
114impl std::fmt::Display for InvalidActionType {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 write!(
117 f,
118 "expected that action entity uids would have the type `Action` but got "
119 )?;
120 join_with_conjunction(f, "and", self.euids.iter(), |f, e| write!(f, "`{e}`"))
121 }
122}
123
124#[derive(Debug, PartialEq, Diagnostic, Error)]
126pub enum InstantiationError {
127 #[error("failed to instantiate template: no value provided for `{slot}`")]
129 MissedSlot {
130 slot: ast::SlotId,
132 },
133}
134
135impl From<ast::UnexpectedSlotError> for FromJsonError {
136 fn from(err: ast::UnexpectedSlotError) -> Self {
137 Self::TemplateToPolicy(err.into())
138 }
139}