Improve type error message when referencing functions #8253
Improve type error message when referencing functions #8253johanfylling merged 5 commits intoopen-policy-agent:mainfrom
Conversation
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| allow if { | ||
| some x in f | ||
| }`, | ||
| expectedError: "policy.rego:8: rego_type_error: function data.play.f[__local1__] used as reference, not called", |
There was a problem hiding this comment.
Can we perhaps go the extra mile and remove the [__local1__], too? If it's easy to do, figure out the prefix that is a function vs the tail of the ref... If it's cumbersome, forget about it, the new message is so much better than the old message already. This is just icing on the cake (I like icing on the cake).
There was a problem hiding this comment.
Here you go some icing! Added a check to match the last part of the reference to the regex __local[0-9]+__ and drop it. Wish there was more context identifying what part is the function 🤔
but this does get us:
1 error occurred: test.rego:8: rego_type_error: function data.play.f used as reference, not called
data.play.f[__local1__]
^^^^^^^^^^^
have: (any) => any
There was a problem hiding this comment.
There is also this logic that wraps the __local1__ in brackets that could be repurposed: https://github.com/open-policy-agent/opa/blob/main/v1/ast/term_appenders.go#L198-L223
Although it is vague enough that I am not sure what all the scenarios would be when something is wrapped in brackets. So I prefer the approach of handling this specific scenario until we find an example it doesn't work.
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
resolve #6840
For the example provided in the issue, the new error will be:
This does give the user more information that
fis a function, unfortunately couldn't figure out a way to make__local1__easier to understand.