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

Fix error message generation for parse-only expressions #693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions interpreter/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package interpreter

import (
"fmt"
"strings"

"github.com/google/cel-go/common/containers"
"github.com/google/cel-go/common/types"
Expand Down Expand Up @@ -301,7 +302,14 @@ func (a *absoluteAttribute) Resolve(vars Activation) (any, error) {
}
}
}
return nil, missingAttribute(a.String())
var attrNames strings.Builder
for i, nm := range a.namespaceNames {
if i != 0 {
attrNames.WriteString(", ")
}
attrNames.WriteString(nm)
}
return nil, missingAttribute(attrNames.String())
}

type conditionalAttribute struct {
Expand Down Expand Up @@ -436,7 +444,9 @@ func (a *maybeAttribute) AddQualifier(qual Qualifier) (Attribute, error) {
}
}
// Next, ensure the most specific variable / type reference is searched first.
a.attrs = append([]NamespacedAttribute{a.fac.AbsoluteAttribute(qual.ID(), augmentedNames...)}, a.attrs...)
if len(augmentedNames) != 0 {
a.attrs = append([]NamespacedAttribute{a.fac.AbsoluteAttribute(qual.ID(), augmentedNames...)}, a.attrs...)
}
return a, nil
}

Expand Down Expand Up @@ -1288,7 +1298,7 @@ func (e *resolutionError) Error() string {
return fmt.Sprintf("index out of bounds: %v", e.missingIndex)
}
if e.missingAttribute != "" {
return fmt.Sprintf("no such attribute: %s", e.missingAttribute)
return fmt.Sprintf("no such attribute(s): %s", e.missingAttribute)
}
return "invalid attribute"
}
Expand Down
2 changes: 1 addition & 1 deletion interpreter/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ func TestAttributesOptional(t *testing.T) {
varName: "a",
quals: []any{},
vars: map[string]any{},
err: errors.New("no such attribute: id: 1, names: [a]"),
err: errors.New("no such attribute(s): a"),
},
}
for i, tst := range tests {
Expand Down
13 changes: 13 additions & 0 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,19 @@ var (
expr: `1/0 in [1, 2, 3]`,
err: `division by zero`,
},
{
name: "list_index_error",
expr: `mylistundef[0]`,
unchecked: true,
err: `no such attribute(s): mylistundef`,
},
{
name: "pkg_list_index_error",
container: "goog",
expr: `pkg.mylistundef[0]`,
unchecked: true,
err: `no such attribute(s): goog.pkg.mylistundef, pkg.mylistundef`,
},
}
)

Expand Down