Skip to content

Commit

Permalink
Improve error message for template slot mismatch (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
svanderbleek authored Nov 9, 2023
1 parent 379b236 commit 0487415
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cedar-policy-core/src/parser/cst_to_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ impl RefKind for SingleEntity {

impl RefKind for EntityReference {
fn err_str() -> &'static str {
"entity uid or template slot"
"entity uid or matching template slot"
}

fn create_slot(_: &mut ParseErrors) -> Option<Self> {
Expand Down Expand Up @@ -1639,7 +1639,7 @@ impl ASTNode<Option<cst::Primary>> {
if slot.matches(var) {
Ok(T::create_slot(errs))
} else {
Err(format!("?{slot}"))
Err(format!("{slot} instead of ?{var}"))
}
}
cst::Primary::Literal(_) => Err("literal".to_string()),
Expand Down Expand Up @@ -3774,13 +3774,21 @@ mod tests {
#[test]
fn is_err() {
let invalid_is_policies = [
(
r#"permit(principal == ?resource, action, resource);"#,
"?resource instead of ?principal",
),
(
r#"permit(principal, action, resource == ?principal);"#,
"?principal instead of ?resource",
),
(
r#"permit(principal in Group::"friends" is User, action, resource);"#,
"expected a entity uid or template slot",
"expected a entity uid or matching template slot",
),
(
r#"permit(principal, action, resource in Folder::"folder" is File);"#,
"expected a entity uid or template slot",
"expected a entity uid or matching template slot",
),
(
r#"permit(principal is User == User::"Alice", action, resource);"#,
Expand All @@ -3800,11 +3808,11 @@ mod tests {
),
(
r#"permit(principal is User in 1, action, resource);"#,
"expected a entity uid or template slot, found a `literal` statement",
"expected a entity uid or matching template slot, found a `literal` statement",
),
(
r#"permit(principal, action, resource is File in 1);"#,
"expected a entity uid or template slot, found a `literal` statement",
"expected a entity uid or matching template slot, found a `literal` statement",
),
(
r#"permit(principal is 1, action, resource);"#,
Expand All @@ -3824,11 +3832,11 @@ mod tests {
),
(
r#"permit(principal is User in ?resource, action, resource);"#,
"expected a entity uid or template slot",
"expected a entity uid or matching template slot",
),
(
r#"permit(principal, action, resource is Folder in ?principal);"#,
"expected a entity uid or template slot",
"expected a entity uid or matching template slot",
),
(
r#"permit(principal, action, resource) when { principal is 1 };"#,
Expand Down

0 comments on commit 0487415

Please sign in to comment.