error
Function
error — wrap a Zed value as an error
Synopsis
error(val: any) -> error
Description
The error function returns an error version of any value.
It wraps the value val
to turn it into an error type providing
a means to create structured and stacked errors.
Examples
Wrap a record as a structured error:
yield error({message:"bad value", value:this})
{foo:"foo"}
Loading...
echo '{foo:"foo"}' \
| super -z -c 'yield error({message:"bad value", value:this})' -
Wrap any value as an error:
yield error(this)
1
"foo"
[1,2,3]
Loading...
echo '1
"foo"
[1,2,3]' \
| super -z -c 'yield error(this)' -
Test if a value is an error and show its type “kind”:
yield {this,err:is_error(this),kind:kind(this)}
error("exception")
"exception"
Loading...
echo 'error("exception")
"exception"' \
| super -z -c 'yield {this,err:is_error(this),kind:kind(this)}' -
Comparison of a missing error results in a missing error even if they are the same missing errors so as to not allow field comparisons of two missing fields to succeed:
badfield:=x | yield badfield==error("missing")
{}
Loading...
echo '{}' \
| super -z -c 'badfield:=x | yield badfield==error("missing")' -