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 when using an invalid object key #1193

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions ast/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ func ValueToInterface(v Value, resolver Resolver) (interface{}, error) {
return buf, nil
case Object:
buf := map[string]interface{}{}
err := v.Iter(func(k, v *Term) error {
err := v.Iter(func(k, value *Term) error {
ki, err := ValueToInterface(k.Value, resolver)
if err != nil {
return err
}
asStr, stringKey := ki.(string)
if !stringKey {
return fmt.Errorf("object value has non-string key (%T)", ki)
return fmt.Errorf("object value (%v) has non-string key of type (%T), value (%v)", v, ki, ki)
}
vi, err := ValueToInterface(v.Value, resolver)
vi, err := ValueToInterface(value.Value, resolver)
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ func TestUnversionedGetHealth(t *testing.T) {
}
}

func TestInvalidKeyV1(t *testing.T) {
testPolicy := `package foo

g[app] = zone {
app = ["foo"]
zone = 456
}
`
f := newFixture(t)

if err := f.v1(http.MethodPut, "/policies/foo", testPolicy, 200, ""); err != nil {
t.Fatalf("Unexpected error while creating policy: %v", err)
}

if err := f.v1(http.MethodGet, "/data/foo", "", 500, `{
"code": "internal_error",
"message": "object value ({[\"foo\"]: 456}) has non-string key of type ([]interface {}), value ([foo])"
}`); err != nil {
t.Fatalf("Unexpected error from GET: %v", err)
}
}

func TestDataV0(t *testing.T) {
testMod1 := `package test

Expand Down