Skip to content

Commit

Permalink
chore: Update error message for invalid expression (#2156)
Browse files Browse the repository at this point in the history
Makes the error message consistent with others by moving the compile
errors to the description.

Signed-off-by: Charith Ellawala <charith@cerbos.dev>

---------

Signed-off-by: Charith Ellawala <charith@cerbos.dev>
  • Loading branch information
charithe committed May 16, 2024
1 parent c7e7860 commit 1a25664
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
7 changes: 6 additions & 1 deletion internal/compile/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package compile

import (
"fmt"
"strings"

"github.com/google/cel-go/cel"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
Expand Down Expand Up @@ -61,7 +62,11 @@ func compileMatch(modCtx *moduleCtx, path string, match *policyv1.Match, markRef
func compileCELExpr(modCtx *moduleCtx, path, expr string, markReferencedVariablesAsUsed bool) *exprpb.CheckedExpr {
celAST, issues := conditions.StdEnv.Compile(expr)
if issues != nil && issues.Err() != nil {
modCtx.addErrForProtoPath(path, newCELCompileError(expr, issues), "Invalid expression: `%s`", expr)
errList := make([]string, len(issues.Errors()))
for i, ce := range issues.Errors() {
errList[i] = ce.Message
}
modCtx.addErrForProtoPath(path, newCELCompileError(expr, issues), "Invalid expression `%s`: [%s]", expr, strings.Join(errList, ", "))
return nil
}

Expand Down
7 changes: 1 addition & 6 deletions internal/compile/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ func newCELCompileError(expr string, issues *cel.Issues) *CELCompileError {
}

func (cce *CELCompileError) Error() string {
errList := make([]string, len(cce.issues.Errors()))
for i, ce := range cce.issues.Errors() {
errList[i] = ce.Message
}

return fmt.Sprintf("failed to compile `%s` [%s]", cce.expr, strings.Join(errList, ", "))
return "invalid expression"
}

func (cce *CELCompileError) Unwrap() error {
Expand Down
4 changes: 2 additions & 2 deletions internal/test/testdata/compile/bad_cel_expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
wantErrors:
- file: principal_policies/donald_duck_20210210.yaml
error: |-
failed to compile `resource.attr.dev_record ^^ true` [Syntax error: token recognition error at: '^', Syntax error: token recognition error at: '^', Syntax error: extraneous input 'true' expecting <EOF>]
invalid expression
description: |-
Invalid expression: `resource.attr.dev_record ^^ true`
Invalid expression `resource.attr.dev_record ^^ true`: [Syntax error: token recognition error at: '^', Syntax error: token recognition error at: '^', Syntax error: extraneous input 'true' expecting <EOF>]
position:
line: 12
column: 15
Expand Down
7 changes: 3 additions & 4 deletions internal/test/testdata/compile/yaml_comment_in_condition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ mainDef: "resource_policies/leave_request_20210210.yaml"
wantErrors:
- file: resource_policies/leave_request_20210210.yaml
error: |-
failed to compile `# YAML comment
request.resource.attr.status == "PENDING_APPROVAL"` [Syntax error: token recognition error at: '#', Syntax error: mismatched input 'comment' expecting <EOF>]
invalid expression
description: |-
Invalid expression: `# YAML comment
request.resource.attr.status == "PENDING_APPROVAL"`
Invalid expression `# YAML comment
request.resource.attr.status == "PENDING_APPROVAL"`: [Syntax error: token recognition error at: '#', Syntax error: mismatched input 'comment' expecting <EOF>]
position:
line: 15
column: 11
Expand Down

0 comments on commit 1a25664

Please sign in to comment.