Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for template slot mismatch #411

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions cedar-policy-core/src/parser/cst_to_ast.rs
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