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 #516

Closed
andy-styra opened this issue Nov 29, 2017 · 2 comments · Fixed by #2577
Closed

Improve error message when using an invalid object key #516

andy-styra opened this issue Nov 29, 2017 · 2 comments · Fixed by #2577

Comments

@andy-styra
Copy link

When using a non-string value as an object key, OPA returns an internal error, rather than a more specific error pointing out the error.

This can be reproduced using the following policy:

package foo

g[key] = value {
    key = ["foo"]
    value = []
}

Response from OPA:

curl localhost:8181/v1/data/foo
{
    "code": "internal_error",
    "message": "object value has non-string key ([]interface {})"
}
@BenderScript
Copy link
Contributor

Looked at this one, we can improve the message. For example. for the policy below:

`package foo

g[app] = zone {
	app = ["foo"]
	zone = 456
}
`

The message would be:

"object value ({["foo"]: 456}) has non-string key of type ([]interface {}), value ([foo])"

The "[]interface {}" comes from the fact that during parsing ["foo"] is considered an object of type []interface{} meaning basically we do not know what it is so consider it to map[]interface{}

I will submit a pull

@BenderScript
Copy link
Contributor

We will not merging the changes due to:

#1193 (review)

@tsandall tsandall self-assigned this Apr 12, 2019
@tsandall tsandall removed their assignment Jun 20, 2019
@tsandall tsandall added usability and removed bug labels Jun 20, 2019
tsandall added a commit to tsandall/opa that referenced this issue Jul 24, 2020
Previously, when OPA attempted to convert an ast.Object to interface{}
it would error if the ast.Object contained any keys that were not
ast.String values. This behaviour was implemented because JSON only
supports object keys as strings.

This commit changes the conversion implementation to simply JSON
marshal non-string object keys when they are encountered. This way
we avoid runtime errors which can be difficult to debug.

Fixes open-policy-agent#516

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
tsandall added a commit that referenced this issue Jul 27, 2020
Previously, when OPA attempted to convert an ast.Object to interface{}
it would error if the ast.Object contained any keys that were not
ast.String values. This behaviour was implemented because JSON only
supports object keys as strings.

This commit changes the conversion implementation to simply JSON
marshal non-string object keys when they are encountered. This way
we avoid runtime errors which can be difficult to debug.

Fixes #516

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants