open-policy-agent/opa

Improve `rego_type_error` error message

Opened this issue · 0 comments

As one of the most common errors, we should strive to make type error messages as easy to understand as possible.
Simplified / silly example, but it'll work well for demonstration:

package policy

x := "10" / 2

There are several things that could be improved in the error message:

1 error occurred: policy.rego:3: rego_type_error: div: invalid argument(s)
	have: (string, number, ???)
	want: (x: number, y: number, z: number)
  1. Use of the "internal" function names like div. Saying "division", using / as it exists in the policy (or both) would be less leaky, and easier to understand.
  2. That the last argument represents the return value is not something most users are aware of, and the docs aren't going to be of much help here, as this "syntax" is rather internal. It would be good if the return value was represented separately. Also the "???" easily makes it seem that's where the error is, while it in fact is rather irrelevant in this case.
  3. The arguments in "have" aren't named, while "want" uses named arguments. This inconsistency is confusing, and we should use the same form for both sides.
  4. In addition to the "have"/"want" forms, a human-readable message like "expected first argument in division to be number, got string" would go a long way to help quickly point out the actual error. Perhaps this breaks down for more complex cases, but it'd be fine to leave those out if so.