The built-in function error
can be used to create a custom error message to
be displayed when a field is in error.
In a disjunction
You can use error
in a disjunction, like this:
// A is in error without a custom error message.
A: 1 | 2
A: string
// B is the same as A with a custom error message.
B: 1 | 2 | error("Custom error B")
B: string
n: 0
// C is in error without a custom error message.
C: 42 / n
// D is the same as C with a custom error message.
D: 42/n | error("Custom error D")
$ cue vet file.cue
A: 2 errors in empty disjunction:
A: conflicting values string and 1 (mismatched types string and int):
./file.cue:2:4
./file.cue:3:4
A: conflicting values string and 2 (mismatched types string and int):
./file.cue:2:8
./file.cue:3:4
B: Custom error B:
./file.cue:6:12
C: failed arithmetic: division by zero:
./file.cue:11:4
D: Custom error D:
./file.cue:13:11
If at least one element of a disjunction is valid then any custom errors are ignored; otherwise all custom error messages are reported together.
As an assertion
The error
function can also be used as an assertion:
A: [1, 2, 3]
B: 10
if len(A) < B {
A: error("must have at least \(B) elements")
}
$ cue vet file.cue
A: must have at least 10 elements:
./file.cue:4:5
However,
while this is valid CUE, we’re not yet sure if it should be considered idiomatic.
At present, using error
in this way is neither recommended nor discouraged.
Feedback
Because error
was only introduced recently
(in CUE v0.14.0)
best practices around its usage are still evolving.
We’d love to hear how you’re using error
, and if it behaves how you expect –
please do join
the CUE community and tell us about your experience on
Slack,
Discord, or in
a GitHub discussion!
Related content
- Reference: The CUE Language Specification:
the built-in function
error
- Language Tour: Disjunctions