cedar_policy_core/est/err.rs
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
/*
* 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.
*/
use crate::ast;
use crate::entities::json::err::JsonDeserializationError;
use crate::parser::err::{parse_errors, ParseErrors};
use crate::parser::unescape;
use miette::Diagnostic;
use nonempty::NonEmpty;
use smol_str::SmolStr;
use thiserror::Error;
/// Errors arising while converting a policy from its JSON representation (aka EST) into an AST
//
// This is NOT a publicly exported error type.
#[derive(Debug, Diagnostic, Error)]
pub enum FromJsonError {
/// Error while deserializing JSON
#[error(transparent)]
#[diagnostic(transparent)]
JsonDeserializationError(#[from] JsonDeserializationError),
/// Tried to convert an EST representing a template to an AST representing a static policy
#[error(transparent)]
#[diagnostic(transparent)]
TemplateToPolicy(#[from] parse_errors::ExpectedStaticPolicy),
/// Tried to convert an EST representing a static policy to an AST representing a template
#[error(transparent)]
#[diagnostic(transparent)]
PolicyToTemplate(#[from] parse_errors::ExpectedTemplate),
/// Slot name was not valid for the position it was used in. (Currently, principal slots must
/// be named `?principal`, and resource slots must be named `?resource`.)
#[error("invalid slot name or slot used in wrong position")]
#[diagnostic(help(
"principal slots must be named `?principal` and resource slots must be named `?resource`"
))]
InvalidSlotName,
/// EST contained a template slot for `action`. This is not currently allowed
#[error("slots are not allowed for actions")]
ActionSlot,
/// EST contained a template slot in policy condition
#[error(transparent)]
#[diagnostic(transparent)]
SlotsInConditionClause(#[from] parse_errors::SlotsInConditionClause),
/// EST contained the empty JSON object `{}` where a key (operator) was expected
#[error("missing operator, found empty object")]
MissingOperator,
/// EST contained an object with multiple keys (operators) where a single operator was expected
#[error("found multiple operators where one was expected: {ops:?}")]
MultipleOperators {
/// the multiple operators that were found where one was expected
ops: Vec<SmolStr>,
},
/// Error thrown while processing string escapes
// show just the first error in the main error message, like in [`ParseErrors`]; see #326 and discussion on #477
#[error("{}", .0.first())]
UnescapeError(#[related] NonEmpty<unescape::UnescapeError>),
/// Error reported when the entity type tested by an `is` expression cannot be parsed.
#[error("invalid entity type: {0}")]
#[diagnostic(transparent)]
InvalidEntityType(ParseErrors),
/// Error reported when the extension function name is unknown. Note that
/// unlike the Cedar policy format, the JSON format has no way to distinguish
/// between function-style and method-style calls.
#[error("invalid extension function: `{0}`")]
UnknownExtensionFunction(ast::Name),
/// Returned when an entity uid used as an action does not have the type `Action`
#[error(transparent)]
#[diagnostic(transparent)]
InvalidActionType(#[from] parse_errors::InvalidActionType),
}
/// Errors arising while converting a policy set from its JSON representation (aka EST) into an AST
#[derive(Debug, Diagnostic, Error)]
pub enum PolicySetFromJsonError {
/// Error reported when a policy set has duplicate ids
#[error(transparent)]
#[diagnostic(transparent)]
PolicySet(#[from] ast::PolicySetError),
/// Error reported when attempting to create a template-link
#[error(transparent)]
#[diagnostic(transparent)]
Linking(#[from] ast::LinkingError),
/// Error reported when converting an EST policy or template to an AST
#[error(transparent)]
#[diagnostic(transparent)]
FromJsonError(#[from] FromJsonError),
}
/// Errors while linking a policy
#[derive(Debug, PartialEq, Diagnostic, Error)]
pub enum LinkingError {
/// Template contains this slot, but a value wasn't provided for it
#[error("failed to link template: no value provided for `{slot}`")]
MissedSlot {
/// Slot which didn't have a value provided for it
slot: ast::SlotId,
},
}
impl From<ast::UnexpectedSlotError> for FromJsonError {
fn from(err: ast::UnexpectedSlotError) -> Self {
Self::TemplateToPolicy(err.into())
}
}