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::unescape;
20use smol_str::SmolStr;
21use thiserror::Error;
22
23/// Errors arising while converting EST to AST
24#[derive(Debug, Error)]
25pub enum EstToAstError {
26    /// Error while deserializing JSON
27    #[error(transparent)]
28    JsonDeserializationError(#[from] JsonDeserializationError),
29    /// Tried to convert an EST representing a template to an AST representing a policy
30    #[error("tried to convert JSON representing a template to a Policy rather than Template: {0}")]
31    TemplateToPolicy(#[from] ast::ContainsSlot),
32    /// Slot name was not valid for the position it was used in. (Currently, principal slots must
33    /// be named `?principal`, and resource slots must be named `?resource`.)
34    #[error("invalid slot name, or used in wrong position")]
35    InvalidSlotName,
36    /// EST contained a template slot for `action`. This is not currently allowed
37    #[error("specified a slot for `action`")]
38    ActionSlot,
39    /// EST contained the empty JSON object `{}` where a key (operator) was expected
40    #[error("Missing operator, found empty object")]
41    MissingOperator,
42    /// EST contained an object with multiple keys (operators) where a single operator was expected
43    #[error("Found multiple operators where one was expected: {ops:?}")]
44    MultipleOperators {
45        /// the multiple operators that were found where one was expected
46        ops: Vec<SmolStr>,
47    },
48    /// At most one of the operands in `a * b * c * d * ...` can be a non-{constant int}
49    #[error("Multiplication must be by a constant int. Neither {arg1} nor {arg2} is a constant")]
50    MultiplicationByNonConstant {
51        /// First non-constant argument
52        arg1: ast::Expr,
53        /// Second non-constant argument
54        arg2: ast::Expr,
55    },
56    /// Error thrown while processing string escapes
57    #[error("{:?}", .0.iter().map(|e| e.to_string()).collect::<Vec<String>>())]
58    UnescapeError(Vec<unescape::UnescapeError>),
59}
60
61/// Errors while instantiating a policy
62#[derive(Debug, PartialEq, Error)]
63pub enum InstantiationError {
64    /// Template contains this slot, but a value wasn't provided for it
65    #[error("failed to instantiate template: no value provided for {slot}")]
66    MissedSlot {
67        /// Slot which didn't have a value provided for it
68        slot: ast::SlotId,
69    },
70}