cedar_policy_core/est/err.rs
1/*
2 * Copyright 2022-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 crate::ast;
18use crate::entities::JsonDeserializationError;
19use crate::parser::err::ParseErrors;
20use crate::parser::unescape;
21use smol_str::SmolStr;
22use thiserror::Error;
23
24/// Errors arising while converting a policy from its JSON representation (aka EST) into an AST
25#[derive(Debug, Error)]
26pub enum FromJsonError {
27 /// Error while deserializing JSON
28 #[error(transparent)]
29 JsonDeserializationError(#[from] JsonDeserializationError),
30 /// Tried to convert an EST representing a template to an AST representing a static policy
31 #[error("tried to convert JSON representing a template to a static policy: {0}")]
32 TemplateToPolicy(#[from] ast::UnexpectedSlotError),
33 /// Slot name was not valid for the position it was used in. (Currently, principal slots must
34 /// be named `?principal`, and resource slots must be named `?resource`.)
35 #[error("invalid slot name or slot used in wrong position. Principal slots must be named `?principal` and resource slots must be named `?resource`")]
36 InvalidSlotName,
37 /// EST contained a template slot for `action`. This is not currently allowed
38 #[error("slots are not allowed for actions")]
39 ActionSlot,
40 /// EST contained the empty JSON object `{}` where a key (operator) was expected
41 #[error("missing operator, found empty object")]
42 MissingOperator,
43 /// EST contained an object with multiple keys (operators) where a single operator was expected
44 #[error("found multiple operators where one was expected: {ops:?}")]
45 MultipleOperators {
46 /// the multiple operators that were found where one was expected
47 ops: Vec<SmolStr>,
48 },
49 /// At most one of the operands in `a * b * c * d * ...` can be a non-{constant int}
50 #[error(
51 "multiplication must be by a constant int: neither `{arg1}` nor `{arg2}` is a constant"
52 )]
53 MultiplicationByNonConstant {
54 /// First non-constant argument
55 arg1: ast::Expr,
56 /// Second non-constant argument
57 arg2: ast::Expr,
58 },
59 /// Error thrown while processing string escapes
60 #[error("invalid escape patterns: {:?}", .0.iter().map(|e| e.to_string()).collect::<Vec<String>>())]
61 UnescapeError(Vec<unescape::UnescapeError>),
62 /// Error reported when the entity type tested by an `is` expression cannot be parsed.
63 #[error("invalid entity type: {0}")]
64 InvalidEntityType(ParseErrors),
65}
66
67/// Errors while instantiating a policy
68#[derive(Debug, PartialEq, Error)]
69pub enum InstantiationError {
70 /// Template contains this slot, but a value wasn't provided for it
71 #[error("failed to instantiate template: no value provided for `{slot}`")]
72 MissedSlot {
73 /// Slot which didn't have a value provided for it
74 slot: ast::SlotId,
75 },
76}