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

docs: Add section on built-in function errors #4024

Merged
merged 1 commit into from
Nov 17, 2021
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
43 changes: 43 additions & 0 deletions docs/content/policy-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,49 @@ them to avoid naming conflicts, e.g., `org.example.special_func`.
See the [Policy Reference](../policy-reference#built-in-functions) document for
details on each built-in function.

### Errors

By default, built-in function calls that encounter runtime errors evaluate to
undefined (which can usually be treated as `false`) and do not halt policy
evaluation. This ensures that built-in functions can be called with invalid
inputs without causing the entire policy to stop evaluating.

In most cases, policies do not have to implement any kind of error handling
logic. If error handling is required, the built-in function call can be negated
to test for undefined. For example:

```live:eg/errors:module:merge_down
allow {
io.jwt.verify_hs256(input.token, "secret")
[_, payload, _] := io.jwt.decode(input.token)
payload.role == "admin"
}

reason["invalid JWT supplied as input"] {
not io.jwt.decode(input.token)
}
```
```live:eg/errors:input:merge_down
{
"token": "a poorly formatted token"
}
```
```live:eg/errors:output
```

If you wish to disable this behaviour and instead have built-in function call
errors treated as exceptions that halt policy evaluation enable "strict built-in
errors" in the caller:

API | Flag
--- | ---
`POST v1/data` (HTTP) | `strict-builtin-errors` query parameter
`GET v1/data` (HTTP) | `strict-builtin-errors` query parameter
`opa eval` (CLI) | `--strict-builtin-errors`
`opa run` (REPL) | `> strict-builtin-errors`
`rego` Go module | `rego.StrictBuiltinErrors(true)` option
Wasm | Not Available

srenatus marked this conversation as resolved.
Show resolved Hide resolved
## Example Data

The rules below define the content of documents describing a simplistic deployment environment. These documents are referenced in other sections above.
Expand Down
2 changes: 2 additions & 0 deletions docs/content/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ The path separator is used to access values inside object and array documents. I
- **explain** - Return query explanation in addition to result. Values: **full**.
- **metrics** - Return query performance metrics in addition to result. See [Performance Metrics](#performance-metrics) for more detail.
- **instrument** - Instrument query evaluation and return a superset of performance metrics in addition to result. See [Performance Metrics](#performance-metrics) for more detail.
- **strict-builtin-errors** - Treat built-in function call errors as fatal and return an error immediately.

#### Status Codes

Expand Down Expand Up @@ -830,6 +831,7 @@ The request body contains an object that specifies a value for [The input Docume
- **explain** - Return query explanation in addition to result. Values: **full**.
- **metrics** - Return query performance metrics in addition to result. See [Performance Metrics](#performance-metrics) for more detail.
- **instrument** - Instrument query evaluation and return a superset of performance metrics in addition to result. See [Performance Metrics](#performance-metrics) for more detail.
- **strict-builtin-errors** - Treat built-in function call errors as fatal and return an error immediately.

#### Status Codes

Expand Down